Simple XML parsing with CLI PHP, character encoding problems

January 7th, 2007 by ltz

When using PHP for creating html documents intended to be read by a web browser there’s the possibility to set character encoding within the html-heading, getting a extra way to handla the character presentation of the content. However when running PHP code through the CLI-PHP software for internal, server-side purposes handling data the character encoding might require a little bit more work. Reading XML-documents using the PHP “xml_parser” and at the same time trusting the character encoding given in the XML-document header as shown below might not be enough in order to make sure you get the correct character encoding.

...xml version="1.0" encoding="iso-8859-1" ...

Be sure to set the encoding within the creation of the XML-parser.


$xml_parser = xml_parser_create();

xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");

Posted in Programming |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Pierre Norraeus
Joomla! 1.5 Beta 2 - Red Barchetta