Using the following characters: " & ' < > £
for testing. My code builds an XML file using PHP and DOMDocument.
<?php
$xml = new DOMDocument();
$xml->formatOutput = true;
$root = $xml->createElement('Start_Of_XML');
$xml->appendChild($root);
$el = $xml->createElement($node,htmlspecialchars(html_entity_decode($value[$i],ENT_QUOTES,'UTF-8'),ENT_QUOTES,'UTF-8'));
$parent->appendChild($el);
?>
The htmlspecialchars()
method above converts these chars to:
" & ' < > £
resp. That is, the double quote, apostrophe and pound sign fail to get encoded.
If I adjust the code to use htmlentities() instead:
<?
$el = $xml->createElement($node,htmlentities(html_entity_decode($value[$i],ENT_QUOTES,'UTF-8'),ENT_QUOTES,'UTF-8'));
?>
The chars get parsed as :
" & ' < > £
So the pound sign gets converted along with the rest, but again the quote and apostrophe fail to get encoded when the XML is saved.
After searching through several posts I'm at a loss to find a solution?
Edit:
Using Gordon's answer as a basis I got the results I was looking for using something along the lines of https://3v4l.org/ZksrE
Great effort from ThW though. Seems pretty comprehensive. I'm going to accept this as a solution. Thanks.