why isn\'t this working as expected:
echo str_replace(\"é\",\"é\",\"Fédération Camerounaise de Football\");
result:
\"F
You are doing it wrong. This string is not incorrect and in need of replacement, it is simply encoded with UTF-8.
All you have to do is utf8_decode('Fédération Camerounaise de Football')
.
You are seeing Fédération Camerounaise de Football
as output because you are double passing your data in UTF-8.
Observe:
file1.php saved in UTF-8 format:
Output:
Fédération Camerounaise de Football
Now, if you tell the browser you are using UTF-8, it should display the content straight:
file2.php saved in UTF-8 format:
Output:
Fédération Camerounaise de Football
Perfect.
Howover, you are doing things even worse. You have an UTF-8 encoded string, and is encoding it again, by writing it to a UTF-8 encoded file.
file3.php saved in UTF-8 format:
Output:
Fédération Camerounaise de Football
What a mess. Let's make it worse by seeing if we can fix this with str_replace
:
file4.php saved in UTF-8 format:
Output:
Fédération Camerounaise de Football
As you can see, we "fixed" it. Sort of. Thats what you are doing. You are transforming é
into é
, even though you are not seeing this because your editor won't let you see the real symbols behind the encoding, but the browser does.
Let's try this again with ASCII:
file5.php saved in ASCII format:
Output:
Fédération Camerounaise de Football
Magic! The browser got everything right now. But whats the real solution? Well. If you have a string hardcoded in your PHP file, then you should simply write Fédération Camerounaise de Football
instead of placing the god damn thing wrong. But if you are fetching it from another file or a database, you should take one of the two courses:
Use utf8_decode()
to transform the data you fetch into your desired output.
Don't transform anything and use header('Content-Type: text/html; charset=utf-8');
to tell the browser you are printing content in UTF-8 format, so it will display things correctly.