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 <p>Résumé<p>
and the browser renders <p>Résumé<p>
. 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 do 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.
Use mysqli_real_escape_string() to Insert Special Characters Into a Database in PHP. To get user input with special characters from the form fields, we use the mysqli_real_escape_string() function. We need the following parameters: database connection and the strings we want to escape.
Special characters in HTML are represented by entities such as & and < . There are two PHP functions that turn special characters in a string into their entities: one for removing HTML tags, and one for extracting only meta tags.
Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
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, use the ISO-8859-1
charset:
<?php
/*Just for your server-side code*/
header('Content-Type: text/html; charset=ISO-8859-1');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"><!-- Your HTML file can still use UTF-8-->
<title>Untitled Document</title>
</head>
<body>
<?= "àè" ?>
</body>
</html>
This will print correctly: àè
I have a little better understanding now. The reason this works is that the client (browser) is being told, through the response header()
, to expect an ISO-8859-1
text/html file. (As others have mentioned, you can also do this by updating your .ini
or .htaccess
files.) Then, once the browser begins to parse that given file into the DOM, the output will obey any <meta charset="">
rule but keep your ISO characters intact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With