Kanchan
Kanchan

Reputation: 1619

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.?

Upvotes: 0

Views: 5336

Answers (3)

laplagam
laplagam

Reputation: 11

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.

Upvotes: 0

Kanchan
Kanchan

Reputation: 1619

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".

Upvotes: 0

Kevin Cittadini
Kevin Cittadini

Reputation: 1469

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">

Upvotes: 2

Related Questions