I'm trying to grasp UTF-8 and charset encoding in general, but it's tricky
This code:
<?php
header('Content-type: text/html; charset=utf-8');
?>
<!doctype html>
<html lang="da">
<head>
<title>UTF-8 test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<p>UTF-8 æøå tést Señor!</p>
</div>
</body>
</html>
Outputs:
UTF-8 ��� t�st Se�or!
Same result on both local server and on multiple public sites (all Apache)
Do I need to change something in php? Or Apache? Or my text editor? (notepad++)
Your text editor isn't saving as UTF-8.
I think you should change the character encoding of your notepad++, you can change this under "Encoding" menu.
you can check your right corner of status bar for the encoding of your file.
Related
I'm trying to change my website from windows-1251 to utf-8, but it won't budge.
There are a ton of "solutions" to this on web, but any doesn't seem to be sufficient.
Here is what I did:
I changed the encoding of the index.html (smarty template) and index.php files to UTF-8.
I set
<html lang="ru">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
in index.html
I set
header('Content-type: text/html; charset=utf-8');
in several places in index.php
I changed
default_charset="UTF-8"
in php.ini and restarted my Apache(XAMPP) server
And still, the Chrome devtools say my page is "Content-Type: text/html; charset=windows-1251" and I see gibberish instead of Cyrillic in my browser.
What else can I do?
P.S. my index.html looks like this now:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Тест
</body>
</html>
and I open it dirrectly via the browser: http://localhost:8080/index.html
Check this url. This might help you.
charset changing
Edit:
Try these.
Set the header before HTML tags.
<?php header('Content-type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
when you connect your script to the database you need to specify the same character set for PHP/MySQL connection.
Check
Old issue
It appeared I had a Chrome encoding extension installed, which was set to Windows-1251: https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfilgdlmhefphglae
Currently I created simple website using include system
header.php - contains first part of HTML page (Head, meta tags, JS codes ... etc )
page.php - contains simple php code
page content
My main problem with arabic language
I have to put
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
in page.php, footer.php between <head> tags otherwise the arabic will not support correctly.
This prevents page validation because of these tags.
Is there any method to avoid this problem ?
Thanks
// Send a raw HTTP header
header ('Content-Type: text/html; charset=UTF-8');
// Declare encoding META tag, it causes browser to load the UTF-8 charset
// before displaying the page.
echo '<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />';
// Right to Left issue
echo '<body dir="rtl">';
Encode the arabic string into UTF-8 using a tool like this. (No need to change any settings - that link has the the correct settings you need).
Then use utf8_decode() to decode the string back.
Example:
<?php echo utf8_decode('your_encoded_string_goes_here'); ?>
Apart from all of the answers... Make sure your (HTML/PHP) files are saved with the right encoding utf-8
All you need is to put this
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
in a file that you include/include_once in your pages
EDIT. example:
header.html
<!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" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>my title in العربية</title>
</head>
<body>
mypage.php
<?php
include_once 'header.html';
?>
<p>
العربية
</p>
<?php
include 'foot.html';
?>
foot.html
<div>my footer</div>
</body>
</html>
1- Put this
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
2- You should also save the documents in UTF-8 not ANSI
Your headers should appear as the following:
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="utf-8">
</head>
<body>
Save your document as utf-8
When I try to display letters of a foreign languages in my php file they show up as "?".
Anybody have any ideas how I can display them properly?
If I save the file as a HTML it displays the letter properly.
To add support for foreign characters you need to use UTF-8 (or any other similar encoding but UTF-8 is the most widely used) encoding.
text editing
At first you need to have your PHP/HTML files in UTF-8 encoding, use a text editor that supports this encoding, just check that the editor doesn't prepend UTF-8 BOM symbols to the file.
PHP
To serve your PHP files as UTF-8 (so the browser doesn't get mixed up about this) add relevant header
<?php header("Content-Type: text/html; Charset=UTF-8"); ?>
HTML
To serve your static HTML pages as UTF-8 use appropriate meta tag
<!-- with HTML5 -->
<meta charset="utf-8" />
<!-- or with older HTML formats -->
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
MySQL
To save/load UTF-8 encoded text in MySQL use the following statement after connecting to the MySQL server
<?php
mysql_connect(...);
mysql_set_charset('utf8');
?>
but check that the tables have also appropriate encodings set
+Using charset=utf-8
<!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>
</head>
<body>
<body>
</body>
++ If you mention about open your php file .you have use an editor that support utf-8 something like this.
I am using Eclipse .
I abstracted the header from a larger set of php files for clarity. When I load it into Wampserver, the <p>é</p> appears as � on the site, despite the header calling for utf-8 charset. What is wrong in this document?
(Note that I tried to modify the encoding by replacing iso-8859-1 with utf-8, that didn't help.)
header.php:
<?php
header('Content-Type:text/html; charset=UTF-8');
echo '<?xml version="1.0" encoding="iso-8859-1"?>'
?>
<!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" xml:lang="fr" lang="fr">
<head>
<title>Blabla</title>
</head>
<body>
<p>é</p>
</body>
</html>
try this<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> in the head section
and also check your file encoding
You are sending two contradicting character sets, iso-8859-1 and utf-8.
If you
fix that and send only one character set, and
encode the actual file in the character set you specify (there should be a character set option in your IDE's or editor's "Save as..." dialog)
it should work.
this worked for me :
I add to the MVC COntroller : produces={"application/json;charset=utf-8"}
test.php - Using regular IDE(phpDesigner7)
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
echo "بوغاليا";
?>
</body>
</html>
output : ???????
test.html - Using regular IDE(phpDesigner7)
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
بوغاليا
</body>
</html>
output : ???????
test.html - Using Arabic Pad
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
بوغاليا
</body>
</html>
output : بوغاليا
hello.php - Using Arabic Pad
<?php
echo "Hello";
?>
output : Nothing
I am running a local WAMP(PHP 5.3). From this.
I have tried saving these pages with : charset=utf-8 as well on both the programs with no luck.
I know for fact that my IDE can't handle special characters and the other program that Can handle special characters can write workable HTML but if PHP files are written in it, It will not work. I know this is an encoding issue(in the IDE and while displaying the pages).
How does one go about displaying special characters with PHP code, I have large chunks of this arabic text that needs to be displayed.
Do you know how is this done?
Thanks! :)
Why are you setting your charset to iso-8859-1? That's Latin-1. You can't render non-Latin characters in Latin-1.
Try using UTF-8 instead.