Turkish characters are not displayed correctly [duplicate] - php

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.

Related

Encode accentuated characters in json using php

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

UTF-8 French accented characters issue

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?

How can I print special HTML characters correctly, using PHP?

I'm trying to print a string, like this:
Noticias de Fútbol.
But when I print this string, it displays like this:
Noticias de F�tbol.
I tried htmlspecialchar() and many more, but my output remains the same.
$str = "Noticias de Fútbol";
$strfoot = html_entity_decode($str);
How can I resolve this?
You don't need to use html_entity_decode, you can just simply print out your $str with echo, but you have to make sure your html has this line of code in the <head>:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
You have use charset utf 8 and htmlentities function of php
Like
<meta http-equiv="content-type" content="text/html; charset=utf-8">
$test=htmlentities("Sisälämpötila");
echo $test;
You are most likely outputting to a page that has the wrong charset specified. To confirm this open a JavaScript console and evaluate window.document.charSet and window.document.characterSet.
If that is the case you can use a meta tag to inform the browser of your intended charset:
<meta content="text/html; charset=utf-8">
I'm reading elsewhere the tag might be:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Your php file is not utf-8 but the browser is reading it as utf-8 which is giving you that question mark character. Save your file as utf-8, most editors/ides have options to set the charset of the file.

croatian language text is not appearing on browser

I have written some croatian language in my page.But when i m seeing result some unsupported text is not coming on browser.This is replacing "?" there.what i have to do ?
I suggest using UTF-8 character encoding.
You should also convert your HTML files to UTF-8 character set (without BOM). You can do this using Notepad++ for example.
I have tested the following code and it is working:
<html>
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
vaše
</body>
</html>
Add encoding to your page, depending on what encoding you used to create your page, you need to add the following:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Or it's HTML5 equivalant
<meta charset="utf-8" />

not being displayed properly

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

Categories