سؤال

I am using php to dynamically display a page. However, it does not display correctly on some characters, for example ♥. I am getting the JSON string using SimpleXML. When I do echo $string, it returns âÂÂ¥. Then, I tried using utf8_decode($string), and I got â¥, which is still wrong. How do I manipulate this string correctly for it to display when I write echo $string?

هل كانت مفيدة؟

المحلول

Try putting this in the <head> of your PHP file:

<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

For HTML5 (thanks to scootergrisen), you can also use:

<meta charset="utf-8">

Edit:

Well, to me, it seems that your API is not encoding properly. This will make any attempts to decode your string fail (and leave you with parsing '♥' yourself).

Your API encodes ♥ as \u00c3\u00a2\u00c2\u0099\u00c2\u00a5, which according to this seems invalid.

Therefore, the only (hackish) solution that I see right now would be to re-parse your API's response yourself, for example like this.

Edit 2:

Whatever it is your API is doing, don't rely on it. You have all the data you need in your XML already (in an unescaped, UTF-8 format), so why not access it directly? :)

This might be the best thing to do without any hackish fixes:

$name = $steamdata->steamID;

نصائح أخرى

First make sure you save your file and present your file to the browser with the same encoding. For example save your PHP file in UTF-8 and add in your HTML5 file <meta charset="utf-8"> in the <head> part.

<!DOCTYPE html>

<head>

   <meta charset="utf-8">

</head>

If it still dont work it might be because your using some PHP functions that dont understand multibyte and thinks 8 bytes = 1 character.

There are some replacement functions. For example mb_substr() instead of substr() if you install the multibyte extionsion during the PHP installation.

But for some functions there is not a replacement but you can try and make one yourself.

I had problems with ucfirst() because there is no mb_ucfirst().

So instead of this which gave me the same problem you have :

function mb_ucfirst($tekst){

   return utf8_encode(ucfirst(utf8_decode($tekst)));

}

I use this :

function my_mb_ucfirst($str){

    $fc = mb_strtoupper(mb_substr($str, 0, 1));

    return $fc . mb_substr($str, 1);

}

Maybe this can help you. Try looking through your code that manipulates the string and disable the linies one by one until the problem changes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top