如果存在';s小于符号后面没有空格


XML text included between less than and greater than symbols is not displayed if there's no space after the less than symbol

当我试图显示从XML文件中提取的小于和大于符号中包含的一些文本时,我注意到了一种奇怪的行为。

尝试以下示例:

$xml = '<doc>
    <one>&lt;&lt;Hello&gt;&gt; "Hello" inside less than and greater than symbols will not be printed</one>
    <two>&lt;&lt; Hello&gt;&gt; "Hello" is printed because there is a space after "&lt;" </two>
</doc>';
$simple = simplexml_load_string($xml);
echo "<pre>";
    print_r($simple);
echo "</pre>";
echo "This works fine: &lt;&lt;Hello&gt;&gt;";

正如您可能注意到的,如果小于符号后没有空格,则不会显示其间的文本,只显示一个小于和一个大于符号。

问题是,我正在处理的XML文件恰好在小于和大于符号之间包含一些文本,而在小于符号之后没有任何空格。顺便说一句,如果你在浏览器中查看源代码(Ctrl-U),文本就会正确显示:

<pre>SimpleXMLElement Object
(
    [one] => <<Hello>> "Hello" inside less than and greater than symbols will not be printed
    [two] => << Hello>> "Hello" is printed because there is a space after "<" 
)
</pre>
This works fine: &lt;&lt;Hello&gt;&gt;

它被打印出来,但没有在浏览器中显示,因为它将其解释为html标记。您需要为打印的所有节点使用htmlspecialchar转义html字符。