简体   繁体   English

html实体不转换特殊字符

[英]html entities not converting special characters

I'm using htmlentities which is converting characteres with accents, but it is not converting this type of quotes “. 我正在使用htmlentities来转换带重音符号的字符,但未转换这种类型的引号“。 Instead the browser shows a weird symbol with a question mark 相反,浏览器显示带有问号的怪异符号。

How can I convert these kind of characteres that display as symbols? 如何转换这些显示为符号的字符? eg The book called Hello Colors is on the table. 例如,桌上有一本名为``Hello Colors''的书。

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 :S

Looks like you have missed charset specification in your browser , 好像您错过了浏览器中的字符集规范,

try adding <meta charset="UTF-8"> this in your webpage head section . 尝试在网页标题部分添加<meta charset="UTF-8"> I previously had an issue like this to display multilingual text in UTF -8 I did the same to solve this issue . 我以前遇到过这样的问题,无法在UTF -8中显示多语言文本,所以我也做了同样的工作来解决此问题。

hope this helps 希望这可以帮助

BTW BTW

for HTML 5 <meta charset="UTF-8"> works 用于HTML 5 <meta charset="UTF-8">

in case of HTML 4 如果是HTML 4

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

and in case of XML you have to specify 如果是XML,则必须指定

<?xml version="1.0" encoding="UTF-8"?>

Here is the place where you can get all information 这是您可以获得所有信息的地方

Declaring character encodings in HTML 声明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 有几种设置内容字符集的方法,即使您也可以设置服务器以始终显示utf-8,也可以在服务器设置部分阅读此处以获取更多信息。

EDIT : - 编辑:-

After conversation with you in the comment section , 在评论部分与您交谈后,

Your problem is with Joomla 您的问题出在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 您通过将字符集ISO-8859放到网页中进行了测试,结果证明了您正在以ISO格式(而不是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 . 我认为您的mysql数据库可能不在UTF-8中,这就是为什么它将ISO文本发送到前端,您可以将数据库更改为UTF-8 general-ci或ISO latin1,这是可行的,并且可行,我建议您将DB更改为utf-8-general-ci,因为您已经有标头设置为utf-8的html页面,这将解决您的问题。

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 . 另外,如果您不能更改数据库,那么您已经知道它在ISO字符集中,因此将所有Joomla模板头更改为ISO字符集。

which looks like this 看起来像这样

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

OR 要么

in php 在PHP中

header('Content-Type: text/html; charset=iso-8859-1'); 

by removing your charset utf-8 declaration which is existing . 通过删除现有的charset utf-8声明。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM