简体   繁体   中英

UTF-8 Encoding not working

I know a number of post is there for utf-8 encoding issue. but i'm getting fail to convert string into utf-8.

I have a string "beløp" in php.

When i print this screen in i frame it printed "bel p" .

After that i tried - utf8_encode("beløp"); - now i got output - "bel�p" .

Again i tried iconv("UTF-8", "ISO-8859-1", "beløp"); now i got output - "bel " .

And finally i tried - utf8_encode(utf8_decode("beløp")); now i got output - "bel?p" .

Please let me know where i'm wrong and how i can fix it.?

This

bel�p

is an indication that you are outputting a non-UTF-8 character in a UTF-8 context.

Make sure your file is encoded in UTF-8 ( Don't know what editor you're using, but Notepad++/Sublime Text got a "Save with encoding.." option ) and if at the top of your HTML page there's

<meta charset="utf-8">

Hi it's fixed there was problem in my file it was not encoded in "UTF-8".

I fixed by replacing "bel p" to "beløp".

The reason your conversion does not work is because the original format of your "beløp" text was not in iso-8859-1. The utf8_encode will only work for conversions is from this format. What could work for this type of issues is to use mb_detect_encoding function ( http://php.net/manual/en/function.mb-detect-encoding.php ) to find out which format the text is originally from, then use the iconv convert from the detected encoding to utf-8. When this is done you have to make sure as mentioned on earlier comments that utf-8 is as encoding in the header.

Note that the php mb detect enconding is not very reliable and can make mistakes on detecting correct encoding. Especially if you do not have a large amount of text. To ensure to display all text correct at all times you need to make sure that all processing at all times is in the same encoding. If you get the text from external sources or web services you should always check the headers for correct encoding before the text is processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM