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 .
Related
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
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.
I am working on a php based site where some texts are greek and some are english. Greek texts are not showing correctly. The file is saved in UTF-8. Is there anything special to do in the header of the html page to show the greek text correctly? My header is like -
<!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" />
Also when I submit a form containing greek text, I am using -
$firstname = mysql_real_escape_string($_POST['firstname']);
Is this okay to use "mysql_real_escape_string" here?
Try the following:
Set the collation of your database/ tables/ rows to UTF-8. UTF8_general_ci should do.
Set the connection between MySQL and PHP to UTF-8. (By executing the query SET NAMES 'utf8' after connecting or by setting the default connection encoding).
Try sending the content-type header with PHP: header("Content-Type: text/html; charset=utf-8");.
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"}
As explained here (PHP Include and accents (They show up as �)) php has a strange behavior processing the accents. My question is Why?
I mean: I have a simple utf-8 charset page. With this:
<!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>My Title</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php include ('file.php');?>
The included file just says: "Administración." It has no charset, just header tags (h2, h3...), and some links. Like this:
<h2>Administración</h2>
So, there is no charset conflict. Are not they supposed that the include files are just included?
The previous question was answered with some fix to the problem, but my question is Why PHP behaves this way?
to answer you new questions (from your comment):
How can I do the same thing in other
editors, how do I know the default
charset?
default-charset and charset for every single file can be set in almost every code-editor i know - where exactly depends on the editor. simply take a look into the manual/documentation of your editor for that.