2012-09-17 33 views
1

url編碼我創建了一個字符串錯誤的xml文件

<?xml version='1.0' encoding='ISO-8859-1'?> 
<response> 
    <content>Question - aa.Reply the option corresponding to your answer(You can vote only once)</content> 
    <options> 
    <option url="http://localhost/poll/index.php?message=vote%3Aaar1%3Asdy&mobile=9747444565" name="sdy"/> 
    <option url="http://localhost/poll/index.php?message=vote%3Aaar1%3Ab&mobile=9747444565" name="b"/> 
    </options> 
</response> 

從下面的PHP代碼 $appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile']);

創建的選項代碼的網址屬性,但是當我將其轉化爲XML,我收到以下錯誤。

此頁面包含以下錯誤:在240欄第1行

錯誤:的EntityRef:期待 ';' 下面是頁面渲染到第一個錯誤。

這是爲什麼happening.I敢肯定,這是URL的問題encoding.So是什麼網址的正確方法encoding.I意味着 什麼樣的變化,應適用於URL編碼

$appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile']); 

獲取參數和值是 $_GET['message'] = "vote:".$kwd.":".$oopt $_GET['mobile'] = 888888errt434

回答

2

您在URL中有一個未編碼的&(與號)字符。 &是所有基於SGML的標記形式中的一個特殊字符。

htmlspecialchars()將解決這個問題:

htmlspecialchars($appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile'])); 

我個人更喜歡使用DOM創建XML文檔,而不是字符串連接。這也將正確處理SGML特殊字符的編碼。我會做這樣的事情:

// Create the document 
$dom = new DOMDocument('1.0', 'iso-8859-1'); 

// Create the root node 
$rootEl = $dom->appendChild($dom->createElement('response')); 

// Create content node 
$content = 'Question - aa.Reply the option corresponding to your answer (You can vote only once)'; 
$rootEl->appendChild($dom->createElement('content', $content)); 

// Create options container 
$optsEl = $rootEl->appendChild($dom->createElement('options')); 

// Add the options - data from wherever you currently get it from, this array is 
// just meant as an example of the mechanism 
$options = array(
    'sdy' => 'http://localhost/poll/index.php?message=vote%3Aaar1%3Asdy&mobile=9747444565', 
    'b' => 'http://localhost/poll/index.php?message=vote%3Aaar1%3Ab&mobile=9747444565' 
); 
foreach ($options as $name => $url) { 
    $optEl = $optsEl->appendChild($dom->createElement('option')); 
    $optEl->setAttribute('name', $name); 
    $optEl->setAttribute('url', $url); 
} 

// Save document to a string (you could use the save() method to write it 
// to a file instead) 
$xml = $dom->saveXML(); 

Working example

+0

謝謝......它的工作... –

+0

@JinuJD我個人推薦使用DOM爲這樣的事情而不是 - 見上編輯。 – DaveRandom

+0

這是我的新信息..通過使用DOM方式,我可以避免使用htmlspecialchars ..好嗎? –