koen
koen

Reputation: 13757

prevent PHP DOM from decoding

PHP DOM automatically decodes. Eg * is decoded when creating a DOMElement. Is there a way to prevent this. One solution is to preprocess the text and afterwords postprocess it but this seems more like a hack.

example code:

$domDoc = new \DOMDocument();
$domEl = $domDoc->createElement('foo', 'text with * in it');
$domDoc->appendChild($domEl);
echo $domDoc->saveXML();

Upvotes: 1

Views: 696

Answers (1)

Gordon
Gordon

Reputation: 317177

DOMDocument, or rather libxml, has a boolean flag substituteEntities:

Proprietary. Whether or not to substitute entities. This attribute is not part of the DOM specification and is specific to libxml.

However, this won't work for your ASCII entities because they are predefined. There was a bug report asking for this for PHP 5.1.4, which is marked as "Not A Bug", because

Behavior is correct - These are pre-defined entities and substituteEntities has no effect on the behavior of them. See the specs for more info: http://www.w3.org/TR/2004/REC-xml-20040204/#sec-predefined-ent

Also see http://xmlsoft.org/entities.html

Note that at save time libxml2 enforces the conversion of the predefined entities where necessary to prevent well-formedness problems, and will also transparently replace those with chars (i.e. it will not generate entity reference elements in the DOM tree or call the reference() SAX callback when finding them in the input).

Upvotes: 1

Related Questions