When is the right time to use utf8_encode and utf8_decode?

Character encoding has always been a problem for me. I really don't understand when is the right time to use it.

All the databases that I use now are configured with utf8_general_ci, as this seems like a good "general" start. Since then, I have learned in the last five minutes that it is case insensitive. So it’s useful.

But my question is when to use utf8_encode and utf8_decode ? As far as I can see now, if I have a $ _POST form from a table on my site, I need a utf8_encode() value before inserting it into the database.

Then, when I pull it out, I need utf8_decode. This is true? Or am I missing something?

+5
source share
2 answers

utf8_encode and _decode are pretty bad _decode . The only thing these functions do is convert between UTF-8 and ISO-8859-1 encodings. They do the same thing as iconv($str, 'ISO-8859-1', 'UTF-8') and iconv($str, 'UTF-8', 'ISO-8859-1') respectively. There will be no other magic that will require their use.

If you get a UTF-8 encoded string in your browser and want to insert it into UTF-8 into a database using a database connection with utf8 character set, there is no function for any function anywhere in this chain. You are not at all interested in converting encodings, and this should be the goal.

The only time you can use any function is if you need to convert from UTF-8 to ISO-8859-1 or vice versa at any time, because the external data is encoded in this encoding, or the external system expects data in this encoding. But even then, I would prefer the explicit use of iconv or mb_convert_encoding , as it makes it more obvious and explicit what is happening. And on this day and age, UTF-8 should be the default encoding that you use everywhere, so very little is needed for such a conversion.

Cm:

+4
source

Mostly utf8_encode used for Encodes an ISO-8859-1 string to UTF-8 . When you are working on translating one language into another language, you should use this function to avoid displaying some garbage characters.

As when displaying a Spanish character, the script does not recognize the Spanish character for some time, and it displays some garbage symbol instead of the Spanish character.

You can use at this time.

More about this, please follow this link:

http://php.net/manual/en/function.utf8-encode.php

-1
source

All Articles