我在用PHP显示数据库中的某些数据时遇到问题。
目前如何显示-"SSA&s"
它应该如何显示"SSA的"
HTML元标记
meta charset="UTF-8">
PHP代码
$article_title = html_entity_decode(mb_convert_encoding(stripslashes($r->ArticleTitle), "HTML-ENTITIES", 'UTF-8'));
您可以使用这两种方法html_entity_decode()
或htmlspecialchars_decode()
来decode
基本示例:
$string = html_entity_decode("SSA's");
echo $string; // result SSA's
$string = htmlspecialchars_decode("SSA's");
echo $string; // result SSA's
删除html_entity_decode
函数,因为您正在对HTML-ENTITIES 进行双重编码
正如@ChrisBanks所指出的,您也不需要stripslashes
您需要再次调用html_entity_decode
,因为数据存储为双重编码并删除stripslashes
。
$article_title = html_entity_decode(html_entity_decode(mb_convert_encoding($r->ArticleTitle, "HTML-ENTITIES", 'UTF-8')));
您可能需要首先研究数据是如何以双重编码的方式存储在数据库中的。也许htmlentities
在某个地方被呼叫了两次。
添加到评论:
除非出于某种原因确实需要(在某些情况下可能需要),否则不应该存储HTML编码的数据。只有在输出和在网页上渲染时,您才希望使用htmlentities
。