简体   繁体   English

PHP 加载 HTML 将引号 (") 转换为特殊字符

[英]PHP Load HTML Converting quotes (") to special characters

I have an input where users can enter their html which is later displayed as part of the body.我有一个输入,用户可以在其中输入他们的 html,稍后将显示为正文的一部分。 To make sure user entered html is properly closed, I am using this code:为确保用户输入的 html 正确关闭,我使用以下代码:

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($body);
$dom->removeChild($dom->doctype);
$body = $dom->saveHTML();

$body = str_replace('<html><body>', '', $body);
$body = str_replace('</body></html>', '', $body);
$body = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $body)));

Now, if the user entered content contains double quotes (“) or single quotes, this is converting it into something like &acirc;&#128;&#156;现在,如果用户输入的内容包含双引号(“)或单引号,这会将其转换为类似&acirc;&#128;&#156;的内容。 which is being displayed as “ in the html.“中显示为“”。 How do I make sure special characters like quotes are displayed properly?如何确保正确显示引号等特殊字符?

EDIT: Here's a sample of text which is being pasted:编辑:这是正在粘贴的文本示例:

It is called a “buddy list,” “friend list” or “contact list." Users are typically alerted when someone on their list is online.

which is being converting into:正在转换为:

It is called a “buddy list,†“friend list†or “contact list." Users are typically alerted when someone on their list is online.

Try尝试

<?php
$str1 = htmlentities($str, ENT_QUOTES);

$str = html_entity_decode($str1);
?>

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

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