When adding a string that might contain troublesome characters (eg &, <, >), DOMDocument throws a warning, rather than sanitizing the string.
I'm looking for a succinct way to make strings xml-safe - ideally something that leverages the DOMDocument library.
I'm looking for something better than preg_replace
or htmlspecialchars
. I see DOMDocument::createTextNode()
, but the resulting DOMText object is cumbersome and can't be handed to DOMDocument::createElement()
.
To illustrate the problem, this code:
<?php
$dom = new DOMDocument;
$dom->formatOutput = true;
$parent = $dom->createElement('rootNode');
$parent->appendChild( $dom->createElement('name', 'this ampersand causes pain & sorrow ') );
$dom->appendChild( $parent );
echo $dom->saveXml();
produces this result (see eval.in):
Warning: DOMDocument::createElement(): unterminated entity reference sorrow in /tmp/execpad-41ee778d3376/source-41ee778d3376 on line 6
<?xml version="1.0"?>
<rootNode>
<name>this ampersand causes pain </name>
</rootNode>