Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Utf8 Decoding Issue

I have the following address line: Praha 5, Staré Město,

I need to use utf8_decode() function on this string before I can write it to a PDF file (using domPDF lib).

However, the php utf8 decode function for the above address line appears incorrect (or rather, incomplete).

The following code:

<?php echo utf8_decode('Praha 5, Staré Město,'); ?>

Produces this:

Praha 5, Staré M?sto,

Any idea why ě is not getting decoded?

like image 564
Latheesan Avatar asked Jun 20 '13 10:06

Latheesan


People also ask

How to decode UTF-8 in PHP?

The utf8_decode() function is an inbuilt function in PHP which is used to decode a UTF-8 string to the ISO-8859-1. This function decodes back to the encoded string which is encoded with the utf8_encode() function. Parameter: This function accepts single parameter $string which is required.

How to set UTF-8 encoding in PHP?

PHP UTF-8 Encoding – modifications to your php. The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

Does PHP support UTF-8?

The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.

How to decode ISO-8859-1 in PHP?

The utf8_decode() function decodes a UTF-8 string to ISO-8859-1. This function decodes a string, previously encoded with the utf8_encode() function, back to ISO-8859-1.


1 Answers

utf8_decode converts the string from a UTF-8 encoding to ISO-8859-1, a.k.a. "Latin-1".
The Latin-1 encoding cannot represent the letter "ě". It's that simple.
"Decode" is a total misnomer, it does the same as iconv('UTF-8', 'ISO-8859-1', $string).

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

like image 125
deceze Avatar answered Nov 15 '22 17:11

deceze