philtune philtune - 4 years ago 117
PHP Question

How to display special characters in PHP

I've seen this asked several times, but not with a good resolution. I have the following string:

$string = "<p>Résumé</p>";


I want to print or echo the string, but the output will return
<p>R�sum�</p>
. So I try
htmlspecialchars()
or
htmlentities()
which outputs
&lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;
and the browser renders
&lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;
. I want it, obviously, to render this:


Résumé


And I'm using UTF-8:

header("Content-type: text/html; charset=UTF-8");


What am I missing here? Why does echo and print output a
for any special character? To clarify, the string is actually an entire html file stored in a database. The real world application is not just that one small line.

Answer Source

After much banging-head-on-table, I have a bit better understanding of the issue that I wanted to post for anyone else who may have had this issue.

While the UTF-8 character set will display special characters on the client, the server, on the other hand, may not be so accomodating and would print special characters such as à and è as and .

To make sure your server will print them correctly, us the ISO-8859-1 charset:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"><!-- Your HTML file can still use UTF-8-->
<title>Untitled Document</title>
</head>
<body>
<?php
/*Just for your server-side code*/
header('Content-Type: text/html; charset=ISO-8859-1');
echo "àè";
?>
</body>
</html>

This will print correctly: àè

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download