-->

Retain XML code when using PHP XMLWriter::writeEle

2019-07-31 17:01发布

问题:

I have a mySQL table with fields containing copy that's being turned into XML. Some of the copy in the mySQL fields has bold tags around the words:

<b>This will be bold</b>, this won't be.

However, when I come to build my XML document using XMLwriter, the copy ends up looking like this:

&lt;b&gt;This will be bold&lt;/b&gt;, this won't be

Can anyone advise me on how I can avoid character encoding for these tags?

回答1:

I am guessing your code needs to use XMLWriter::writeRaw instead of XMLWriter::text.

Note that this will only be a good solution if you are confident the content in the database will be proper XML. Otherwise, you will need to first run that content through a DOM parser like DOMDocument::loadXML with the DOMDocument::recover flag set, and then export the content with DOMDocument::saveXML and pass it to XMLWriter::writeRaw.



回答2:

Pass your copy through html_entity_decode(), like this:

echo html_entity_decode("&lt;b&gt;This will be bold&lt;/b&gt;, this won't be");