3

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
  • 1,169
  • 3
  • 10
  • 18

3 Answers3

4

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 .

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
  • I have and it still shows the symbol for those quotes – SnitramSD Feb 21 '13 at 17:09
  • @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 – Aravind.HU Feb 21 '13 at 17:15
  • im 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 “ – SnitramSD Feb 21 '13 at 17:17
  • Okay then you can look into this SO posting here it has some solutions for the similar problem http://stackoverflow.com/questions/4722727/htmlspecialchars-ent-quotes-not-working – Aravind.HU Feb 21 '13 at 17:19
  • 1
    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 do you think is happening? :S – SnitramSD Feb 21 '13 at 17:36
  • @SnitramSD That means the generated content is not UTF-8 its `` can you try – Aravind.HU Feb 21 '13 at 17:46
  • Yes 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 Feb 21 '13 at 17:58
  • I tried using this htmlentities($message, ENT_COMPAT, "ISO-8859-1") but it doesn't work. :S – SnitramSD Feb 21 '13 at 18:01
  • Gotcha 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 – Aravind.HU Feb 21 '13 at 18:08
  • Well 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. – SnitramSD Feb 21 '13 at 18:11
  • put 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 Feb 21 '13 at 18:20
  • @SnitramSD I added you problem is with Joomla from comment here and solution also to my posting – Aravind.HU Feb 22 '13 at 01:22
1

Try the following code, it worked for me:

<?php
$message = "“Hello Colors“";
$message = iconv('UTF-8', 'ASCII//TRANSLIT', $message);
echo htmlentities($message);
?>

Result:

&quot;Hello Colors&quot;
Xyborg
  • 11
  • 1
0

My problem was simple:

When I typed it in the Elements tab of the developer panel, it didn't work. But when I typed it into my code and reloaded the page, it worked.

That's because the browser converts it to the right character when it loads the page, but doesn't convert it when you just type in the entity id in the developer panel.

shieldgenerator7
  • 1,507
  • 17
  • 22