While I am using UTF-8 encoding in the PHP file, I am getting some weird types of characters
Say like this:
"conteúdo está"
How can I display it properly?
The data are being taken from a CSV file which is encoded as UTF-8 and not plain ANSII
Thanks in advance.
header('Content-type: text/html; charset=utf-8');
Related
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have been trying to print some characters on my php page and it returns something else, like corrupted characters
like
"joão", ø, ç, etc
echo '<br>não<br>'
the return is
não
instead:
não
is this a problem with econding utf-8?
i have tried this code
header('Content-Type: text/html, charset=utf-8');
but no result
You have a typo in your Content-Type header. It's ...; charset=... with a semicolon.
Note: Your header header('Content-type: text/html, charset=utf-8');,
is the correct header('Content-type: text/html; charset=utf-8');
Your document (php-file) should be saved in utf8 (without bom) or use iso-8859-1 instead of utf8
To save the document in utf8 without boom use notepade++ (select "Convert to utf-8 without BOM"):
or use:
header('Content-Type: text/html; charset=iso-8859-1');
Tip (if you are using database):
If your database is UTF-8, then:
php file converted to utf-8 without bom
set header to utf8 header('Content-type: text/html; charset=utf-8');
If your database is latin1, then:
php file converted to ANSI
set header to iso-8859-1header('Content-type: text/html; charset=iso-8859-1');
Check that your PHP source file is encoded in UTF-8.
try this
header('Content-Type: text/html; charset=iso-8859-1');
The encoding of your php file itself may be wrong. You might want to change it to utf-8.
However, it is not good practice to include special characters directly in your code. Instead, use the HTML charcode equivalents to produce valid output.
Example
echo 'não';
A comprehensive list of HTML character codes can be found here
I have these Chinese characters:
汉字/漢字''test
If I do
echo utf8_encode($chinesevar);
it displays
??/??''test
Or even if I just do a simple
echo $chinesevar
it still displays some weird characters...
So how am I going to display these Chinese characters without using the <meta> tag with the UTF-8 thingy .. or the ini_set UTF-8 thing or even the header() thing with UTF-8?
Simple:
save your source code in UTF-8
output an HTTP header to specify to your browser that it should interpret the page using UTF-8:
header('Content-Type: text/html; charset=utf-8');
Done.
utf8_encode is for converting Latin-1 encoded strings to UTF-8. You don't need it.
For more details, see Handling Unicode Front To Back In A Web App.
Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8
HTML:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
in PHP:
header('Content-Type: text/html; charset=utf-8');
And if you work with a database look that your database is in UTF-8 if you read the text from your database.
$chinesevarOK = mb_convert_encoding($chinesevar, 'HTML-ENTITIES', 'UTF-8');
Perhaps take a look at the following solutions:
Your database, table and field COLLATE should be utf8_unicode_ci
Check if your records are showing the correct characters within the database...
Set your html to utf8
Add the following line to your php after connecting to the database
mysqli_set_charset($con,"utf8");
http://www.w3schools.com/php/func_mysqli_set_charset.asp
save your source code in UTF-8 No BOM
I never had this problem before, it was usually my database or the html page. But now i think its my php. I import text from a csv or from a text area and in both ways it goes wrong.
for example é changes to é. I used htmlentities to fix this but it didn't work. The htmlentities function didn't return é in html but é in html entities, so it already loses the real characters before htmlentities comes in to place... So does that mean my php file has the wrong encoding or something?
I hope someone can help me out..
Thanks!
Chris
A file is usually ISO-8859-1 (Latin) or UTF-8 ... ISO-8859-1 is 1 byte per char, UTF-8 is 1-4 bytes per char. So if you get 2 chars when you expect one, then you are reading UTF-8 and showing it as ISO-8859-1 ... if you get strange chars, then you are reading ISO-8859-1 and showing it as UTF-8.
If you provide more details, it would be easier to pinpoint, but in short, you have inconsistent charsets and need to convert one or the other so they're all the same. But from what it seems, you're using ISO-8859-1 in your project, but you are reading some UTF-8 from somewhere... use utf8_decode($text) if that data should be indeed be stored as UTF-8, or find the data and convert it manually.
EDIT: If you are using AJAX somewhere, then you will ALWAYS get UTF-8 from it, and you'll have to decode it yourself with utf8_decode() if you want to keep using ISO-8859-1.
Try opening your php file and change the encoding to UTF-8
if that doesn't help, add this to your php:
header('Content-Type: text/html; charset=utf-8');
Or this to your html:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Take a look at PHP's iconv().
can anyone tell me what encoding is applied on the chinese character, so that chinese characters are converted into this code or text and stored in mysql database :
ä¸Â`国液化天然æ°â€Ã¨Â¿Â输(控股)有é™Âå…¬å¸控股`
original chinese characters which are displayed in web page :
中国液化天然气运输(控股)有限公司控股
on the web page there is a header function is used to make standard chinese chars as follow:
header('Content-type: text/html; charset=utf-8');
Thanks...
When you decode
中国液化天然气运输(控股)有限公司控股
as UTF-8, and encode as CP-1252, then you get
ä¸å›½æ¶²åŒ–天然气è¿è¾“(控股)有é™å…¬å¸æŽ§è‚¡
When you decode the above as UTF-8 and encode as CP-1252 once again, then you get
ä¸Â国液化天然æ°â€Ã¨Â¿ï¿½Ã¨Â¾â€œÃ¯Â¼Ë†Ã¦Å½Â§Ã¨â€šÂ¡Ã¯Â¼â€°Ã¦Å“‰é™�å…¬å�¸æŽ§è‚¡
That's what here is happening.
It is Unicode character set (code points) encoded as UTF-8.
I got a .vcf file with parts encoded as UTF-8:
CATEGORIES;CHARSET=UTF-8:Straße & –dienste
Now "–" should be a "-" and "Straße" should convert to "Straße".
I tried
utf8_decode()
iconv()
mb_convert_encoding()
And have been playing with several output encoding options like
header('content-type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8');
mb_http_output( "UTF-8" );
But I don't get the wanted results - instead: "StraÃ?e & â??dienste"
Anyone getting that knot out of my brain? Thanks a lot.
solved.
i had to convert the PHP file back to ISO-8859-1 (instead of UTF-8).
thought that would make no difference, but it does!
You may actually want to try utf8_encode(). I had a similar problem when retrieving UTF-8 encoded information from MySQL and displaying it on a UTF-8 HTML page.
forgot to mention: there is no MySQL...
plain php ;-)
echo "Straße & –dienste";
echo utf8_decode("Straße & –dienste");
should somehow become "Straße & -dienste"... but won't, won't, won't
I don't have an answer for you, as I'm not sure I understand fully what you are trying to do (read a .vcf file in PHP?)....
But a clue is this: "Straße" is "Straße" encoded in UTF-8, but then interpreted as Latin1 (or Windows-1252).