I am having problems displaying foreign characters (characters with accents like: é à ù ç and so on)
The row in the database is like this:
Name | Datatype | Charset
title | varchar(255) | utf8_general_ci
I store it like this:
function inputFilter($var)
{
$var = trim(htmlentities(strip_tags($var)));
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = mysql_real_escape_string($var);
return $var;
}
$title = inputFilter($_POST['title']);
and I print it like this:
print $getfromdb['title'];
This is how it's printed out:
Português //Should be: Português
I have tried adding:
htmlspecialchars, utf8_decode/encode and htmlentities to the print, although nothing helps!
I've added this to the header:
<meta charset="utf-8">
What am I doing wrong?
Steps to Follow:
Use the meta tag for UTF8.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8.
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For mysql, you want to convert your table to UTF8.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
Also you can run:
SET NAMES UTF8
as the first query after establishing a connection which will convert your DB connection to UTF8.
Include mysqli_set_charset($link, "utf8"); right after every connection you make. This will work.
Try this:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"> is HTML 5 (and, BTW, UTF-8 is uppercase)
Since it looks like your test chain also involves a form (because of the $_POST), you must make sure that the UTF-8 charset is set for the form too.
Use SET NAMES utf8 before you query/insert into the database
query("SET NAMES utf8");
You should use character encoding as ISO-8859-1 instead of UTF-8 as follows:
<meta charset="ISO-8859-1">
The characters you are trying to show are latin and UTF-8 i.e. UNICODE encoding cannot interpret latin characters.
Reference
And in case of mysql you should use latin1 charset.
Related
I have a PHP file encoded with UTF-8 like this :
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
And before using sql request, I've added this line :
mysql_query("SET NAMES 'utf8'");
My database is coded in UTF-8 and varchar column with latin_swedish_ci
The result is like this picture :
Chang code to this
mysql_query("SET NAMES UTF8");
Save file like this
This is due to incorrect collation / charset of your column. Even if your database characted set is UTF-8, the column's character set takes precedence. To handle special characters like japanese chinese you column should be set to utf8_bin instead of latin_swedish_ci
Some Steps to Follow:
Meta tag for UTF8.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8.
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For MySql, you've to convert your table to UTF8.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
Also run:
SET NAMES UTF8
as the first query after establishing a connection which will convert your DB connection to UTF8.
html encoding in meta tag gets ignored if it is sent via http headers already.
header('Content-Type: text/html; charset=utf-8');
Check from you browser what it thinks the current page encoding is:
Chrome: Tools - Encoding
Firefox: View - Character encoding
MySQL should be ok with just "set names utf8", table dada is encoded automatically to the mysql connections encoding
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')
);
Why insert in database value date as: شهريور ?
How can search this word in database(شهريور)?
In database: structure => date => varchar(255) => utf8_general_ci = "شهريور".
You website uses an encoding in which these characters do not exists, so the browser sends HTML entities instead.
(Try this here: http://codepad.viper-7.com/dfFMvW ; This page is in ISO-8859-1, if you send non-ISO-8859-1 characters in the input, they are sent as HTML entities.)
To avoid this you have to use a different encoding, like UTF-8.
Add this header in your <head> tag:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
Or do this in your PHP before printing anything:
header('Content-Type: text/html; charset=UTF-8');
And make sure your database uses UTF-8 too.
You can convert your database to UTF-8 by doing this:
ALTER DATABASE your_database CHARACTER SET utf8;
-- for each table:
ALTER TABLE some_table CONVERT TO CHARACTER SET utf8;
And after you connect to the database, send this query:
SET NAMES UTF8;
You dont need to html escape those characters as long as you have a UTF* table, and you do.
Simply make sure that the table is UTF8, that the connection is utf8, and the browser reads the texts as utf8.
for mysql see SET CHARACTER SET, SET NAMES, SET COLLATION_CONNECTION
for html use <meta content="text/html;charset=UTF-8" http-equiv="Content-Type" /> and the according http headers, if needed
When I pass a string with special characters to my view, The special characters are shown as a question mark, eg:
$data['make'] = 'Quels pneus Dunlop avez-vous acheté ?';
$this->load->view("form", $data);
This looks as follows in my view:
When I type the characters directly into the HTML page they show fine. how can I fix this issue?
EDIT: The charset is already set to:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Using codeigniters ascii_to_entities function did the trick.
$this->load->helper('text');
ascii_to_entities($string);
Looks like it could be a charset conflict. Make your HTML charset declaration UTF8 and save your data as UTF8 in the database or text file.
In your <head> tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If you are using htmlentities() to output the data var then consider specifying the character set there as well.
echo htmlentities($str, ENT_COMPAT, 'UTF-8');
Save your view page as utf8 and that will do the job
I want to convert special characters like ñ, Ñ to htmlentities using php.
I tried using htmlentities, but instead of returning "ñ" for its value it returns "ñ" as its value.
Make sure that your page charset is set to utf-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
You need to specify the character set you use as the third parameter to htmlentities(). The default character set is iso-8859-1. If you use UTF-8 for your data, you need to say so:
$result = htmlentities($string, ENT_QUOTES, "UTF-8");
You have to specify the charset because the default is ASCII (http://php.net/manual/en/function.htmlentities.php):
htmlentities($stringToConvert, ENT_COMPAT, 'UTF-8')