首页 > 解决方案 > 将html代码转换为纯文本时出现问题

问题描述

我正在尝试将 HTML 代码转换为纯文本作为段落,HTML 代码将由 CKEDITOR 编写。然后我使用strip_tags()andhtml_entity_decode()将字符串转换为普通段落。使用普通句子可以正常工作,但是当您的句子内容中有一些符号作为首字母缩写词时,例如',它不会被转换。我不知道如何理解这个问题。这是一个例子:

*输入CKEDITOR:

This is a notification, but don't worry about this

*PHP:

$message = '<p>This is a notification, but don&#39;t worry about this.</p>';
$message = strip_tags($message);
$message = html_entity_decode($message);
$message = preg_replace("/&#?[a-z0-9]+; | \r|\n/i","",$message);
echo $message;

*输出:

This is a notification, but don&#39;t worry about this.

标签: phpstring

解决方案


默认情况下,html_entity_decode只转换双引号而不是单引号。使用以下方法转换两者:

$message = html_entity_decode($message, ENT_QUOTES);

https://www.php.net/manual/en/function.html-entity-decode.php


推荐阅读