开发者

Extract text including line breaks from XML with PHP

When I extract text from an XML file

Here is some text before the
<br/><br/>
line break.

in PHP,

echo $v开发者_StackOverflowalue->description;

I get the text but not the including br tags. How do I get around this?

Thanks.


And from experience, you shouldn't even get any text after the <br/> tags. Reason for this is because all text nodes in XML are suppose to have < and > replaced with their htmlentity() counterparts, and all other special characters replaced with htmlspecialchars(). I'm fairly certain that it causes an error with your XML DOM parser, or at least make it as a new node, an empty text node with a line break, I think.

The only solution for this is to store the XML into a string, use regex to take out the <br/> tags (well, all the < and > tags for that matter), and replace them with the correct values I noted above.

Or, you can read about CDATA here, and escape the tags instead, but that's if you're the one creating that XML file. You should notify the webmaster for the site that you got the XML from, that the XML is incorrectly created.


First, you can read the XML file into one string, and then replace '
' by '<br/>'. Now, you can load the replaced string as XML data, and process it with XML DOM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜