Home > Programming > PHP DOM method gotcha!

PHP DOM method gotcha!

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!

Chris Fontes Programming

  1. August 4th, 2009 at 05:26 | #1

    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.

  2. Chris Fontes
    August 4th, 2009 at 11:12 | #2

    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!

  1. No trackbacks yet.

Powered by WP Hashcash