php - 将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'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't worry about this.
解决方案
默认情况下,html_entity_decode
只转换双引号而不是单引号。使用以下方法转换两者:
$message = html_entity_decode($message, ENT_QUOTES);
https://www.php.net/manual/en/function.html-entity-decode.php
推荐阅读
- docker - keycloak /opt/jboss/tools/docker-entrypoint.sh 出错:第 47 行:KEYCLOAK_FRONTEND_URL
- c++ - 打印给定数字范围内的素数。我无法使代码工作
- java - 我有一个关于对象数组并用 scan.nextLine() 填充的基本 java 问题
- python - 列出 4 个随机数 (0-9) 可以等于 10 的所有方式(使用任何运算符 +、-、*、/)
- django - 关于路径和自定义装饰器的 Python3 Django 问题
- java - 为什么这会引发空点异常?
- php - php 图像压缩 gif 无损失动画
- python - 我的文本分类器模型没有改善多个类
- node.js - 在他们网站上提供的 eshop 等 total.js 应用程序中调试和查找错误的最佳方法是什么?
- asp-classic - 我想通过 vb 6 将我的一些经典 asp 自定义函数转换为 dll 文件