Php 当文本同时包含“XML”和“XML”时,将文本转换为有效XML的健壮方法&;引用&引用;及&;nbsp&引用;
从中,我知道什么XML不能保存Php 当文本同时包含“XML”和“XML”时,将文本转换为有效XML的健壮方法&;引用&引用;及&;nbsp&引用;,php,xml,parsing,Php,Xml,Parsing,从中,我知道什么XML不能保存 因此,如果我有以下文本 $now = <<<'NOW' <p><span data-farm="{"animal":["dog"]}">Hi </span></p> NOW; 我会得到 PHP Warning: DOMDocument::loadXML(): attributes construct er
因此,如果我有以下文本
$now = <<<'NOW'
<p><span data-farm="{"animal":["dog"]}">Hi </span></p>
NOW;
我会得到
PHP Warning: DOMDocument::loadXML(): attributes construct error in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Couldn't find end of Start Tag span line 1 in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: p line 1 and span in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: zoo-root line 1 and p in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Extra content at the end of the document in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
Not valid XML
原因是,在html\u entity\u decode
和utf8\u encode
之后,我们将得到
<p><span data-farm="{"animal":["dog"]}">Hi  </span></p>
你好
我尝试了我知道的最后一种方法
方法3-字符串替换
$xml=new\DOMDocument();
if(!$xml->loadXML(“”.str_替换(“”, ;’,$now)。“”)){
打印“无效的XML\n”;
}否则{
打印“有效的XML\n”;
}
似乎有效。但是,方法3解决方案足够健壮吗?您的文本看起来非常像HTML,并且处理得很好 例子:
$now=loadXML($doctype.'.$now.');
echo$dom->saveXML($dom->documentElement);
输出:
你好
PHP Warning: DOMDocument::loadXML(): attributes construct error in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Couldn't find end of Start Tag span line 1 in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: p line 1 and span in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: zoo-root line 1 and p in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
PHP Warning: DOMDocument::loadXML(): Extra content at the end of the document in Entity, line: 1 in /home/cheok/Desktop/test.php on line 17
Not valid XML
<p><span data-farm="{"animal":["dog"]}">Hi  </span></p>
$xml = new \DOMDocument();
if (!$xml->loadXML('<zoo-root>' . str_replace(' ', ' ', $now) . '</zoo-root>')) {
print "Not valid XML\n";
} else {
print "Valid XML\n";
}
$now = <<<'NOW'
<p><span data-farm="{"animal":["dog"]}">Hi </span></p>
NOW;
$dom = new DOMDocument();
$dom->loadHTML($now, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
echo $dom->saveHTML();
<p><span data-farm='{"animal":["dog"]}'>Hi </span></p>
$now = <<<'NOW'
<p><span data-farm="{"animal":["dog"]}">Hi </span></p>
NOW;
$doctype = '<!DOCTYPE zoo-root [<!ENTITY nbsp " ">]>';
$dom = new DOMDocument();
$dom->substituteEntities = true; // Not required, just for demonstration.
$dom->loadXML($doctype . '<zoo-root>' . $now . '</zoo-root>');
echo $dom->saveXML($dom->documentElement);
<zoo-root><p><span data-farm="{"animal":["dog"]}">Hi </span></p></zoo-root>