Pregunta

i have this string :

dsfssdsdfdsf « ظ…ط¯ظˆظ†ط© ط£ط­ظ…ط¯ ط§ظ„ط®ظˆط§ط¬ط©

it's encoded in UTF-8 , i extracted it from a remote page , which i fetched it's contents using file_get_contents function , the remote page encoding is UTF-8 ,

now i want to insert this string into database which it's table encoding is latin1 , to display it into html page which has charset ISO-8859-1 ,

i tried many times to convert it's encoding from UTF-8 to ASCII , or TO ISO-8859-1 and also other encoding like Windows-1256 , but it doesn't success , the string appear with strange characters , notice that the default string contain arabic characters ,

examples of failed result of encoding :

dsfssdsdfdsf « ظ…ط¯ظˆظ†ط© ط£ط­ظ…ط¯ ط§ظ„ط®ظˆط§ط¬ط©

dsfssdsdfdsf « ãÏæäÉ ÃÍãÏ ÇáÎæÇÌÉ

¿Fue útil?

Solución

If you have UTF-8 strings, store them either as UTF-8 or binary into the database, so the database should have a column that does not change the encoding of it. Using a ISO-8859-1 encoding would destroy the string.

In your HTML page that has ISO-8859-1 encoding, re-encode the string from the database then to ISO-8859-1 for the characters that are supported and for all other characters, use HTML entities. On function in PHP that is able to is mb_convert_encoding:

$usascii = mb_convert_encoding($utf8, 'HTML-ENTITIES', 'UTF-8');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top