InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Handling Content Encoding in PHP
PHP

Handling Content Encoding in PHP

InfinityCoder December 8, 2016

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

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

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

Then convert the data back when you are finished:

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

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:

1
$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:

1
2
3
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:

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

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

1
Gdel, Escher, Bach

The best solution is to use //TRANSLIT:

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

This produces a better-looking string:

1
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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Detecting SSL in PHP
You want to know if a request arrived over SSL. …

Detecting SSL in PHP

Finding PEAR Packages
You want a listing of PEAR packages. From this list …

Finding PEAR Packages

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap