PHP: Encoding problem
Web agency » Digital news » PHP: Encoding problem

PHP: Encoding problem

It is not uncommon to encounter errors related to encoding. Encoding, a real pleasure that we all try to overcome by harmonizing the type of our files in UTF-8. If today solving encoding problems can be done with one or two clicks, this problem can sometimes be more recalcitrant.

One encoding to rule them all!

I was recently confronted (again… ) with an encoding problem. I had to develop a system to extract data from an .xls or .xlsx in PHP. Obviously Microsoft prefers to use its encoding system rather than UTF-8.
I offer you this call which will allow you to return any string, encoded in any ISO, to the format of your choice, in this case UTF-8. The call detects the encoding, and converts it to a function.

1
mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string));

To have access to these PHP functions, you only need to make sure that the php_mbstring extension is activated in your php.ini. This extension is provided in PHP natively which will save you a download.

★ ★ ★ ★ ★