PHP DOM method gotcha!
July 2nd, 2009
Ok, so here is a “gotcha” with PHP’s DOM… if you use the method createTextNode(), and the string you pass happens to have HTML chars, the method will AUTOMATICALLY convert to html special chars. Guess they should call the method createTEXTnode instead!
That is expected behaviour. The DOM classes are an abstraction of the underlying file format, so you shouldn’t have to bother with the encoding details. The XML character entities is just an encoding format, the data (the text) is still the same. If createTextNode() didn’t convert special characters, the resulting XML file could be malformed.
See, I feel differently. I would rather be concerned with the encoding myself, and let the DOM object methods play stupid, unless I explicitly tell it otherwise. Personally, I don’t like automation in methods or functions. Thanks for your comment though! I appreciate insight!