2012-09-17 34 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 ..好吗? –