且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:如何处理<![CDATA [和SimpleXMLElement?

更新时间:2023-11-13 15:12:10

您可能无法正确访问它.您可以直接输出它或将其转换为字符串. (在此示例中,投射是多余的,因为无论如何回声都会自动进行投射)

You're probably not accessing it correctly. You can output it directly or cast it as a string. (in this example, the casting is superfluous, as echo automatically does it anyway)

$content = simplexml_load_string(
    '<content><![CDATA[Hello, world!]]></content>'
);
echo (string) $content;

// or with parent element:

$foo = simplexml_load_string(
    '<foo><content><![CDATA[Hello, world!]]></content></foo>'
);
echo (string) $foo->content;


使用LIBXML_NOCDATA可能会带来更好的运气:


You might have better luck with LIBXML_NOCDATA:

$content = simplexml_load_string(
    '<content><![CDATA[Hello, world!]]></content>'
    , null
    , LIBXML_NOCDATA
);