Php Htmlentities And Saving The Data In Xml Format
Solution 1:
Try to remove the line:
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
Because the text passed to createTextNode() is escaped anyway.
Update: If you want the utf-8 characters to be escaped. You could leave that line and try to add the $string directly in createElement().
For example:
$title = $doc->createElement('title', $string);
$title = $root->appendChild($title);
In PHP documentation it says that $string will not be escaped. I haven't tried it, but it should work.
Solution 2:
It is the htmlentities that turns a &
into &
When working with xml data you should not use htmlentities, as the DOMDocument will handle a &
and not &
.
As of php 5.3 the default encoding is UTF-8, so there is no need to convert to UTF-8.
Solution 3:
This line:
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
… encodes a string as HTML.
This line:
$text = $doc->createTextNode($string);
… encodes your string of HTML as XML.
This gives you an XML representation of an HTML string. When the XML is parsed you get the HTML back.
how can I prevent this from happening?
If your goal is to store some text in an XML document. Remove the line that encodes it as HTML.
Looks like a double encoding.
Pretty much. It is encoded twice, it just uses different (albeit very similar) encoding methods for each of the two passes.
Post a Comment for "Php Htmlentities And Saving The Data In Xml Format"