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

时间:2019-10-18 18:11:56

标签: php html encoding character-encoding

我有一个输入,用户可以输入其html,稍后将其显示为正文的一部分。为了确保正确输入用户输入的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)));

现在,如果用户输入的内容包含双引号(“)或单引号,这会将其转换为类似&acirc;&#128;&#156;的形式,并在html中显示为“。如何确保引号等特殊字符正确显示?

编辑: 这是粘贴的文本示例:

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

正在转换为:

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

1 个答案:

答案 0 :(得分:0)

尝试

sort
相关问题