Reading text in `<![CDATA[…]]>` with SimpleXMLElement [duplicate]

霸气de小男生 提交于 2019-11-30 22:03:58
IMSoP

SimpleXML reads CDATA nodes absolutely fine. The only problem you're having is that print_r, var_dump, and similar functions don't give an accurate representation of SimpleXML objects, because they are not implemented fully in PHP.

If you run echo $myNode->description you will see the content of the CDATA section just fine. The reason is that when you ask for a SimpleXMLElement to be converted to a string, it automatically combines all the text and CDATA content for you - but until you do, it remembers the distinction.

As a general case, to extract the string content of any element or attribute in SimpleXML, cast to string with (string)$myNode. This also prevents other issues, such as functions complaining about getting an object when they were expecting a string, or failure to serialize when saving to a session.

See also my previous answer at https://stackoverflow.com/a/13830559/157957

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!