I am having problem in displaying the in my web page, after using utf8_decode() in PHP it gets displayed as �.
i have been using
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I just noticed, all the other special characters, like ® , ™ etc are also not working.
Be sure that you've specified UTF-8 encoding in your HTML document's tag:
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
That's strange since utf8_encode(' ')===' '. Regardless of whether it's utf8 or latin1 encoded the byte-sequence for is the same.
Is the remaining string properly utf8 encoded?
edit: Why do you use utf8_decode() (converting utf8 encoded strings to latin1) in the first place when you're telling the browser that your page is utf8 encoded?
Have you checked the encoding of the php file itself?
In some windows editors (like notepad++) you can have some utf-8 character problems when you check the wrong encoding for your file - even if you set your meta tag correctly.
In notepad++ you can change it in this section:
Change notepad++ file encoding http://img198.imageshack.us/img198/9081/notepadp.png
If you're not using notepad++, we'll need some more detailed information from your setup, like Operating System used, IDE, etc.
Also make sure you give the document a proper dtd definition by putting something like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
As the first line of html in your php file.
When you use utf8_decode, the string that is passed to this function where is it loaded from? Are you loading data from database? Do you have any included files? If so, check that they are all encoded as GmonC wrote. Try to echo somewhere in page and see if it will show correctly. If not, try to make clean .php file and than see if problem still occurs. If not than some included file could be the problem because it could have different encoding
Related
I've a query result that contains some accentuated characters Like :
CollectionTitle => Afleuréss
But when i write a json file with json_encode($Result_Array) and retrieve the result it shows :
CollectionTitle => NULL
then i used array_map() :
$res[] = array_map('utf8_encode', $row);
But it results me :
CollectionTitle => Afleuréss instead of CollectionTitle => Afleuréss
Please suggest me better way to resolve this issue.
Thanks
The second one is actually the correct one. The problem is your browser cannot detect the encoding and defaults to whatever the default is (probably ISO-8859-1). Switch your browser encoding and you'll see the right character appear.
Add to your HTML head:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
Note that you should have a proper HTML doctype because browsers default to non utf8. You can do a simple test, like I did, this works:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$title = "Jérôme";
echo $title."<br>";
But the place for the meta tag is in the head tag. The HTML document should look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<?php
$title = "Jérôme";
echo $title."<br>";
?>
That is standard.
json_encode only supports UTF-8, but the rest of your app is using Windows-1252. I don't suggest using utf8_encode as that converts from ISO-8859-1 to UTF-8. That only works 95% of the time for you because you are using Windows-1252, not ISO-8859-1*.
I don't know if it's possible for you but if you can, you should switch over to UTF-8 so you don't need this fragile conversion code anywhere.
*This is probably confusing. Browsers do not actually allow you to use ISO-8859-1 and instead treat it as Windows-1252. Same with MySQL, Latin1 means Windows-1252. Both are defaults. utf8_encode/decode of course use actual ISO-8859-1, so it's incompatible in the 0x80-0x9F range.
In your second example / step:
$res[] = array_map('utf8_encode', $row);
It looks like you're trying to encode in UTF-8 something that is not ISO-8859-1.
You should detect / know what's the encoding coming from your Database, and transcode it properly to UTF-8 with iconv for example.
As an alternative, you should know:
What is the encoding in the Database?
What is the encoding of the PHP files?
What is the encoding in the HTML page? <meta charset="utf-8">
And if that's possible, move all of the above to UTF-8...
When i see data as stored on mysql database using phpmyadmin, the characters are stored exactly as é à ç however when i use php to display these data on an html document that has the exact following structure:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
</body>
</html>
I got square instead of accented character, however, i don't have this issue with any accented characters on static content that haven't been loaded from mysql in the same page.
when i see on the source code of the page they seem to be identical! for example:
part of static data on the source code displayed as:
éçà
part of mysql origin data:
éçà
i tried replacing
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
with
<meta http-equiv="Content-Type" content="text/html; charset=windows-1552" />
and as result i got mysql one fixed, static with squares !
any hints?
This is quite common charset issue, you need to set connection encoding manually for MySQL connection (those should be first queries you execute after establishing connection):
SET NAMES utf8;
SET CHARACTER SET utf8;
And also make sure every table has CHARACTER SET set to UTF-8.
Or you could also update server configuration.
Looks like a misconfiguration issue. Most probably your DB or drivers are not using UTF-8.
The fact that the data that comes from the DB shows OK when you change to windows-1552 and the static files do not can mean that your source file is (correctly) in UTF-8, but the data from your DB is arriving in the wrong encoding format.
Whatever is going on, stick to UTF-8.
UPDATE: There is a thread that explains how to automatically set the encoding for the connection:
Change MySQL default character set to UTF-8 in my.cnf?
OK so I have a PHP file with several strings of text in various languages. For most languages like French or Spanish I just simply type in the characters.
The problem I have is with Russian language characters. The PHP file is encoded in UTF-8, how can I make sure that the Russian characters are both saved correctly and displayed correctly on the output web page... Is it just a case of pasting the text into the PHP file, or is there a way to guarantee the characters will be saved into the file correctly - perhaps converting it into HTML-like notation for example?
Obviously I am assuming the end user will have the correct encoding set in their web browser, I just want to make sure I got it all covered from my end.
I am using Notepad++ on Windows to edit my PHP file.
Thanks!
If you want to tell browsers your encoding, place it inside your <header> tag:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
Or short version
<meta charset='utf-8'>
That should be pretty enough for Russian characters to be correctly displayed on a webpage.
if your doctype is html declare <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> but if your doctype is xhtml then declare <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />.Never assume that end-user will act correctly during your designsIf you already have some document, edit your document's meta tag for charset declaration and use notepad++ encoding>convert to UTF-8 without BOM, save your document, safely go on with your multilingual structure from now on.php tag is irrelevant for your question since you don't mention about any database char setting.
There is no difference between Latin and Cyrillic characters in UTF-8. Both are just byte sequences. Configure your server or PHP script to send Content-Type: text/html;charset=utf, and you are rather safe.
Your editor might have problems when the font you are using does not contain Russian characters. Choose another font then.
And please ignore the <meta> element recommendations. You don't need that: it is useless when your HTTP headers are correct, and maybe harmful if they aren’t.
Well you have to check 2 things
To ensure that *.php is an UTF-8 file I use PSPad. If file is not in UTF-8, I save
it like that: http://stepolabs.com/upload/utf-8.png
Then your website must have UTF-8 encoding in <meta> tag;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
... more about metatagging.
Finally if everything is done well - (format and meta declaration) all should be displayed properly!
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
MySql database uses utf-8 encoding and data are stored correctly.I use set_name utf8 query to make sure the data called are utf-8 encoded.all variables from database works fine as long as the header charset is utf-8,but the static html characters do not work properly.When i set header charset to ISO-8859-9 variables are displayed differenly while html characters work ok.can anyone help me?
Utf8 encoding: http://oi48.tinypic.com/289kje1.jpg html tags have problem displaying values.but php data(green ones e.g. ağişler) that come from database is ok.ISO encoding http://oi50.tinypic.com/287n2ph.jpg this time the html is ok but php data are displayed wrong.
<?php
header('Content-Type: text/html; charset=ISO-8859-9');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>noname</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
You can try explicitly adding content type at the top of your file as below
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and remove the below from your code..
header('Content-Type: text/html; charset=ISO-8859-9');
Update:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo "for example Ş and İ "; ?>
</body>
</html>
The above code gives me the below output:
So data from the database works if you tell the browser to interpret it as UTF-8, that means that data is UTF-8 encoded. The HTML code works if you tell the browser to interpret it as ISO-8859, that means the HTML is encoded in ISO-8859.
So you have a document with mixed encodings. Bring those two together. Save your source code files as UTF-8 and tell the browser to interpret it as UTF-8, or get your data from the database in ISO-8859 and tell the browser to interpret it thusly.
Let's say I'm including a file which contains html. The html have characters as exclamation symbols, Spanish accents (á, ó). The parsed included text gets processed as symbols instead of their correct value. This happens on FF but not on IE (8).
I have tried the following functions:
htmlspecialchars, htmlentities, utf8_encode
include htmlentities("cont/file.php");
Sample file.php contents:
<div>Canción, “Song Name”</div>
Output:
Canci�n, �Song Name�
Your code does nothing but to run the string "cont/fie.php" through htmlentities(), the content of the file is not affected by that.
You should set your encoding to UTF-8 on HTML page you are viewing this content on. htmlentities isn't affecting this text at all.
I tried the same stuff with following code and it worked fine:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>TODO supply a title</title>
</head>
<body>
<p>
TODO write content
<?php
include "test.php";
?>
</p>
</body>
</html>
test.php
<div>ääääääó</div>
Output an HTTP Content-Type header that specifies the character encoding you are using (UTF-8 is recommended) in the charset parameter.
echo htmlentities(file_get_contents("cont/file.php")); is what you're probably asking.
But, as mentioned before, you must not use htmlentities but UTB-8 encoding
This is what end up working on two different your code and mine doing the trick; the reason being hard to know but something with parsing.
This is browser showed (FF + IE)-->
alt text http://i77.photobucket.com/albums/j65/speedcoder/4-3-20101-22-31PM.png
Sample** ('include' function not use, so Output Buffer not needed):
<?php
$varr = '<div>ääääääó</div>';
echo utf8_encode($varr);
?>
This one didn't work for me:
<?php
include "test.php";
?>
If the above sample using an include file with html code it didn't convert at least for me the characters. I changed it to not been include file and worked with the utf8_encode, but the problem is that my code needs where using include function which din't work.
The next sample below uses include method and output buffer which allowed code to be rendered and parsed before utf8_encode encoding transpired.
My Code Scenario (for my specific scenario has to be with ob since include file also contains code which needs to be parsed first):
ob_start();
include ("cont/file.php");
$content = ob_get_contents();
ob_end_clean();
echo utf8_encode($content);
Thanks for helping me figure it out "Ondrej Slinták"!!!