html entities not converting special characters
Solution 1
Looks like you have missed charset specification in your browser ,
try adding <meta charset="UTF-8">
this in your webpage head section . I previously had an issue like this to display multilingual text in UTF -8 I did the same to solve this issue .
hope this helps
BTW
for HTML 5
<meta charset="UTF-8">
works
in case of HTML 4
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
and in case of XML you have to specify
<?xml version="1.0" encoding="UTF-8"?>
Here is the place where you can get all information
Declaring character encodings in HTML
There are several ways to setup the content charset , even you can setup your server also to render always utf-8 you can read here for more info in the server setup section
EDIT : -
After conversation with you in the comment section ,
Your problem is with Joomla
you tested by putting charset ISO-8859 in the webpage and it works this clearly proves that you are getting content in ISO not in UTF-8
probabily your mysql Database is not in UTF-8 I think and that is why it is sending ISO text to front , you can change the DB to UTF-8 general-ci or ISO latin1 which ever is feasible and that works I suggest you to change DB to utf-8-general-ci since you already have html pages with header set to utf-8 and that will solve your problem .
Also if you cant change the DB then you already know that its in ISO charset so change all your Joomla template header to ISO charset .
which looks like this
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
OR
in php
header('Content-Type: text/html; charset=iso-8859-1');
by removing your charset utf-8 declaration which is existing .
Solution 2
Try the following code, it worked for me:
<?php
$message = "“Hello Colors“";
$message = iconv('UTF-8', 'ASCII//TRANSLIT', $message);
echo htmlentities($message);
?>
Result:
"Hello Colors"
SnitramSD
Updated on June 05, 2022Comments
-
SnitramSD almost 2 years
I'm using htmlentities which is converting characteres with accents, but it is not converting this type of quotes “. Instead the browser shows a weird symbol with a question mark �
How can I convert these kind of characteres that display as symbols? e.g. The book called �Hello Colors� is on the table.
I've tried this commands but it's not working:
htmlentities($message); htmlentities($message, ENT_QUOTES, 'UTF-8'); htmlentities($message, ENT_NOQUOTES, 'UTF-8'); htmlentities($message, ENT_COMPAT, 'UTF-8');
Thank you.
I just realised something weird, if I do the following
echo $message; die();
to show a white page for debugging the quotes are displayed! So what is happening? Why it's not displaying correctly in the website page? :S
-
SnitramSD about 11 yearsI have <meta http-equiv="content-type" content="text/html; charset=utf-8"> and it still shows the symbol for those quotes
-
Aravind.HU about 11 years@Snitram Okay if you are using linux can you try preparing a sample script with the htmlentities function and a sample string. And run that and see what its rendering for you . This is to make sure whether htmlentities is doing its job or not
-
SnitramSD about 11 yearsim using windows microsoft IIS as the web server. htmlentities is working since it converts the characteres with accents like ù á ì but it's just not converting the quotes “
-
Aravind.HU about 11 yearsOkay then you can look into this SO posting here it has some solutions for the similar problem stackoverflow.com/questions/4722727/…
-
SnitramSD about 11 yearsI just realised something weird, if I do the following echo $message; die(); to show a white page for debugging the quotes are displayed! So what do you think is happening? :S
-
Aravind.HU about 11 years@SnitramSD That means the generated content is not UTF-8 its
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
can you try -
SnitramSD about 11 yearsYes it works if I do that. But I'm using joomla so I had to change a joomla core file to change the charset. Is it possible to use any function that displays the string using the iso-8859-1 instead of having to modify the page's charset? thank you.
-
SnitramSD about 11 yearsI tried using this htmlentities($message, ENT_COMPAT, "ISO-8859-1") but it doesn't work. :S
-
Aravind.HU about 11 yearsGotcha your mysql Database is not in UTF-8 i think and that is why it is sending ISO text to front , you can change the DB to UTF-8 general-ci or ISO latin1 which ever is feasible and that works I suggest you to change DB to utf-8-general-ci since you already have html pages with header set to utf-8 and that will solve your problem
-
SnitramSD about 11 yearsWell the problem is that I don't have that kind of permissions to change the database. :S It's a Microsoft SQL Server database. No other possible way? :S thank you.
-
Aravind.HU about 11 yearsput this line in your joomla template folder `header('Content-Type: text/html; charset=iso-8859-1');' index.php file it solves problem you dont need to add this every where
-
Aravind.HU about 11 years@SnitramSD I added you problem is with Joomla from comment here and solution also to my posting