0

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

som
  • 4,650
  • 2
  • 21
  • 36
Kanchan
  • 1,609
  • 3
  • 22
  • 37
  • Is your source file UTF-8 encoded? – Eggplant Jul 20 '13 at 08:03
  • try adding this tag `` – Khanh TO Jul 20 '13 at 08:07
  • [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/), [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Jul 20 '13 at 08:13
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Jul 20 '13 at 08:13

3 Answers3

2

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">
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Kevin Cittadini
  • 1,449
  • 1
  • 21
  • 30
  • 1
    1) Read other people's comments. 2) Write the same things in a new Answer. 3) Profit. :) – Eggplant Jul 20 '13 at 08:20
  • 3
    @Eggplant not sure what you mean - it's the correct answer to the question? (although the meta tag thing seems unnecessary, as he already is outputting UTF-8, the `�` is indicative of that) – Pekka Jul 20 '13 at 08:23
  • 2
    @Eggplant: I'm a web designer, It's my job, and don't have to "get profit from others" to have knowledge :) Read: I also gave him a "how to" save with encoding.. thing that you didn't. Maybe that's why It's burning you? Stay calm dude, It's a help site. What's important is that the guy who made the question solves his problem. I got no profit from this.. I do this for FREE – Kevin Cittadini Jul 20 '13 at 08:23
  • 1
    @Eggplant if you have a problem with the fact that you posted the solution first in a comment - how about not doing that and *writing an answer in the first place?* Comments are fair game for anyone to turn into an answer. Plus it's not *that* original a solution that no one else would come up with it. If you cast a downvote for that reason, please remove it. – Pekka Jul 20 '13 at 08:24
  • @Kevin I took the liberty of amending your answer, feel free to roll back if you don't like it – Pekka Jul 20 '13 at 08:26
  • Wow chill down guys... *profit*, and especially in this form "Step1, Step2, Profit" is a well-know joke on the internet about being *the smart one*, nothing to flame about. You guys are taking that so seriously, why? It's just my approach to take a "Ask a question about the likely-to-be cause of the problem, then make a sure answer about it", and I think this is very helpful for the asker himself especially about those simple issues: it helps him to better understand why, and don't repeat the mistake again. My bad. Peace and Love :) – Eggplant Jul 20 '13 at 08:27
  • @Eggplant I was upset for a moment because it appeared you criticized and downvoted someone for turning a comment into an answer (or not! He probably came up with it himself) which would have been very unfair. I leave comments with the solution all the time, and the implication is that anyone is free to do with them as they please. All is well :) – Pekka Jul 20 '13 at 08:31
  • @Eggplant: I'm fine too, just said that my answer was genuine. It's my jobs, I know these things and they made me crazy when I was a beginner, UTF-8 can be really anoying. – Kevin Cittadini Jul 20 '13 at 08:58
  • Doesn't work for me. Now php file is not even extecuted but it's content is shown instead. – Viktor Joras Jun 11 '18 at 19:03
0

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

Kanchan
  • 1,609
  • 3
  • 22
  • 37
0

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.

laplagam
  • 11
  • 1
  • 5