PHP XML Handling Content Encoding - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Handling Content Encoding - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 15, 2019

PHP XML Handling Content Encoding

PHP XML


Handling Content Encoding

Problem

PHP XML extensions use UTF-8, but your data is in a different content encoding.

Solution

Use the iconv library to convert data before passing it into an XML extension:

         $utf_8 = iconv('ISO-8859-1', 'UTF-8', $iso_8859_1);

Then convert the data back when you are finished:

         $iso_8859_1 = iconv('UTF-8', 'ISO-8859-1', $utf_8);

Discussion

Character encoding is a major PHP weakness, so you can run into problems if you’re trying to use XML extensions with arbitrary encoded data.

For simplicity, the XML extensions all exclusively use the UTF-8 character encoding. That means they all expect data in UTF-8 and output all data in UTF-8. If your data is ASCII, then you don’t need to worry; UTF-8 is a superset of ASCII. However, if you’re using other encodings, you will run into trouble sooner or later.

To work around this issue, use the iconv extension to manually encode data back and forth between your character sets and UTF-8. For example, to convert from ISO-8859-1 to UTF-8:

         $utf_8 = iconv('ISO-8859-1', 'UTF-8', $iso_8859_1);

The iconv function supports two special modifiers for the destination encoding: //TRANSLIT and //IGNORE. The first option tells iconv that whenever it cannot exactly duplicate a character in the destination encoding, it should try to approximate it using a series of other characters. The other option makes iconv silently ignore any unconvertible characters.

For example, the string $geb holds the text Gödel, Escher, Bach. A straight conversion to ASCII produces an error:

          echo iconv('UTF-8', 'ASCII', $geb);

          PHP Notice: iconv(): Detected an illegal character in input string...

Enabling the //IGNORE feature allows the conversion to occur:

          echo iconv('UTF-8', 'ASCII//IGNORE', $geb);

However, the output isn’t nice, because the ö is missing:

          Gdel, Escher, Bach

The best solution is to use //TRANSLIT:

          echo iconv('UTF-8', 'ASCII//TRANSLIT', $geb);

This produces a better-looking string:

          G"odel, Escher, Bach

However, be careful when you use //TRANSLIT, because it can increase the number of characters. For example, the single character ö becomes two characters: " and o.



No comments:

Post a Comment

Post Top Ad