This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP messing with HTML Charset Encoding
We've come across special characters being transformed.
What is causing this? How can we fix it?
For example:
ë becomes ë
Thank you.
Thats a utf-8 character and you can parse it through utf8_encode() and utf8_decode() in PHP
Charset can be set at numerous places.
table charset
field charset
PHP-MySQL connection charset
Apache default charset
and in HTML metainfo
Make sure you use UTF-8 everywhere, and don't forget to setup the connection properly before the first query:
mysql_query("SET NAMES 'utf8'");
Make sure you are setting your charset in HTML document and with PHP header's function.
Also, you could try to make the first query in MySQL to be SET NAMES=UTF8 (SET NAMES utf8 in MySQL?)
If this is the output of a PHP script, I guess you may consider mb_internal_encoding() function.
Or you can fix that by HTML encoding meta tag. Like <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> inside <head>...</head>.
Related
This question already has answers here:
utf8_encode function purpose
(4 answers)
Closed 1 year ago.
I am having trouble with utf8_encode() function.
Here's an example
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
header("Content-Type: text/html; charset=utf-8");
$str = "Şşİğ";
echo utf8_encode($str);
?>
the output i see is
SsIg (third one is a capital i)
if i don't use utf8_encode() this is what i get
ÅÅÄ°Ä
So, this doesn't really work for some languages. It only makes it a bit sense instead of making it right.
Thanks
If the encoding of the string is already UTF8 (as opposed to ISO-8859-1(5)), you need do nothing:
utf8_encode — Encodes an ISO-8859-1 string to UTF-8
Actually, running utf8_encode on a string which is already UTF8 is bound to wreak some kind of havoc.
You say that the file encoding is UTF8, but what you get looks like ISO-8859. So I suspect you have something that's messing up with the encoding chain.
Verify the Content-Type header (i.e. verify that the one you set is, indeed, the one that gets sent), double check the file encoding, and the browser setting as well (it should be either UTF8 or autodetect).
Also, it is quite strange that you should get "SsIg" -- that is definitely not the expected behaviour of UTF8 encoding. It almost seems that something is trying to map your characters back into the ASCII set by mapping them to the most similar ASCII character. I'd therefore also check any proxies or caches or anything in the middle which is in position to manipulate the data sent by your script.
i haven't found the answer at this question browsing around so i guess asking it's ok...
My php code reads from my Mysql Database a string and prints it, here is the code
$sql2=mysql_query("SELECT * FROM Corsi WHERE Nome='$Ln_1[Ln_1]'");
$Ln_1_a = mysql_fetch_array($sql2);
$Ln_1_descr = $Ln_1_a[5];
But special chars, such as 'è' 'à' 'ò' etc. are printed as '�'.
My Mysql Encoding is utf8 and also in my html header i have utf8 encoding, so what is wrong with this?
Thanks in advance
First of all, at the start of your MySQL connection after database selection you should put this query:
mysql_query('SET NAMES utf8');
Then be sure, your page has encoding UTF-8 (I guess you are using HTML, so it should looks like:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
) and also your script file must be saved in UTF8 encoding.
But I have to remind mysql_* functions are deprecated and you should use mysqli_* or PDO instead with prepared statements due to safety of your queries.
Try (after mysql_connect) do mysql_query('SET NAMES utf8'). And also check, if font support these characters.
Use the utf8_decode function
like this
$Ln_1_descr = utf8_decode($Ln_1_a[5]);
Use htmlspecialchars($yourvariable, ENT_NOQUOTES, "UTF-8")
Also verify your MySQL configure file has:
[client]
default-character-set=UTF-8
[mysql]
default-character-set=UTF-8
Also, utf8_encode() and utf8_decode() would work for you as well.
I've recently come across a weird problem about the meta charset.
If I don't set any charset in my header, all accent like é,è,à.. are show correctly (even var from php) except for text comming from my database are replace by a little question marks in a lozenge.
If I set one of those(I tried both of them) charset in my header
<meta http-equiv="Content-Type" content="text/html" charset="iso-8859-15" />
<meta charset="UTF-8">
Text from my database is okay, but all the rest show the little question marks instead of the accent.
My database character set is UTF-8 unicode, and Collation is UTF_8 general_ci.
Note that I'm using smarty, but I did'nt change the charset in config cause his default is UTF-8.
Okay I found a solution, I'm using an ORM, and i only add the charset=utf8 in the setConnection method like this
$config->set_connections(array(
'development' => 'mysql://user:pass#localhost/mydb;charset=utf8')
);
I have some texts in French (containing accented characters such as "é"), stored in a MySQL table whose collation is utf8_unicode_ci (both the table and the columns), that I want to output on an HTML5 page.
The HTML page charset is UTF-8 (< meta charset="utf-8" />) and the PHP files themselves are encoded as "UTF-8 without BOM" (I use Notepad++ on Windows). I use PHP5 to request the database and generate the HTML.
However, on the output page, the special characters (such as "é") appear garbled and are replaced by "�".
When I browse the database (via phpMyAdmin) those same accented characters display just fine.
What am I missing here?
(Note: changing the page encoding (through Firefox's "web developer" menu) to ISO-8859-1 solves the problem... except for the special characters that appears directly in the PHP files, which become now corrupted. But anyway, I'd rather understand why it doesn't work as UTF-8 than changing the encoding without understanding why it works. ^^;)
I experienced that same problem before, and what I did are the following
1) Use notepad++(can almost adapt on any encoding) or eclipse and be sure in to save or open it in UTF-8 without BOM.
2) set the encoding in PHP header, using header('Content-type: text/html; charset=UTF-8');
3) remove any extra spaces on the start and end of my PHP files.
4) set all my table and columns encoding to utf8mb4_general_ci or utf8mb4_unicode_ci via PhpMyAdmin or any mySQL client you have. A comparison of the two encodings are available here
5) set mysql connection charset to UTF-8 (I use PDO for my database connection )
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
or just execute the SQL queries before fetching any data
6) use a meta tag <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7) use a certain language code for French
<meta http-equiv="Content-language" content="fr" />
8) change the html element lang attribute to the desired language
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
and will be updating this more because I really had a hard time solving this problem before because I was dealing with Japanese characters in my past projects
9) Some fonts are not available in the client PC, you need to use Google fonts to include it on your CSS
10) Don't end your PHP source file with ?>
NOTE:
but if everything I said above doesn't work, try to adjust your encoding depending on the character-set you really want to display, for me I set everything to SHIFT-JIS to display all my japanese characters and it really works fine. But using UFT-8 must be your priority
This works for me
Make your database utf8_general_ci
Save your files in N++ as UTF-8 without BOM
Put $mysqli->query('SET NAMES utf8'); after the connection to the database in your PHP file
Put < meta charset="utf-8" /> in your HTML-s
Works perfect.
If your php.ini default_charset is not set to UTF-8, you need to use a Content-type to define your data. Apply the following header at the top of your file(s) :
header("Content-type: text/html; charset=utf-8");
If you have still troubles with encoding, the cause may be one of the following:
a database server charset problem (check encoding of your server)
a database client charset problem (check encoding of your connection)
a database table charset problem (check encoding of your table)
a php default encoding problem (check default_encoding parameter in parameters.ini)
a multibyte missconfigured (see mb_string parameters in parameters.ini)
a <form> charset problem (check that it is sent as utf-8)
a <html> charset problem (where no enctype is set in your html file)
a Content-encoding: problem (where the wrong encoding is sent by Apache).
SET NAMES worked for me.
My issue was in one of my editing pages the field with the foreign characters would not display, on the production web pages there was no problem.
I know you already have an answer. That's great. But strangely none of these answers solved my issue. I'd like to share my answer for the benefit of the others who may encounter the same issues.
I also had the same problems as the OP, with regards to French accents in a multi-lingual application.
But I encountered this issue for the first time when I had to pass (French accented) data as segments in AJAX calls.
Yes, we must have the database set to work with UTF8. But the fact that AJAX calls had query strings (in my case segments, since I'm using CodeIgniter), I had to simply encode the French text.
To do this on the client-side, use the Javascript encodeURI() function with your data.
And to reverse it in PHP, just use urldecode($MyStr) where data was received as parameters.
Hope this helps.
Type something full French signs in your (php) file
Save that file as UTF-8
Paste line beneath into your website header
header('Content-Type: text/html; charset=utf-8');
Page (file) should look good.
If looks good go here for mysql behavior after (SET_NAMES).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in PHP / MySQL
I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.
One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is.
Oh and I already have included
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and this didn't work.
Hopefully you've all the information to help me, if not just say :) Thanks in advance!
Milaan
You need to have everything in utf-8:
The database field
The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
The content type (like you have already)
I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years.
But this is not neccessary when You have everything in the same encoding
source files encoding
table columns collations
web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)
I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering
i.e. if someone inputs html tags they'll be active in your page and so on.