html entities not converting special characters

旧巷老猫 提交于 2019-12-10 22:18:35

问题


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


回答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 .




回答2:


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;


来源:https://stackoverflow.com/questions/15007956/html-entities-not-converting-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!