php - 使用 PHP 清理 HTML 以创建干净的字符串

标签 php html strip-tags

我有一堆 HTML 数据,我正在使用 PHP 将这些数据写入 PDF 文件。在 PDF 中,我希望所有的 HTML 都被剥离和清理。例如:

<ul>
    <li>First list item</li>
    <li>Second list item which is quite a bit longer</li>
    <li>List item with apostrophe 's 's</li>
</ul>

应该变成:

First list item
Second list item which is quite a bit longer
List item with apostrophe 's 's

但是,如果我简单地使用 strip_tags(),我会得到这样的结果:

   First list item&#8232;

   Second list item which is quite a bit
longer&#8232;

   List item with apostrophe &rsquo;s &rsquo;s

还要注意输出的缩进。

关于如何将 HTML 正确清理为没有乱七八糟的空白和奇怪字符的漂亮、干净的字符串的任何提示?

谢谢:)

最佳答案

字符似乎是 html 实体。尝试:

html_entity_decode( strip_tags( $my_html_code ) );

关于php - 使用 PHP 清理 HTML 以创建干净的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10444509/

相关文章:

php - 删除 Woocommerce 中管理添加订单上的国家/地区账单和运输字段

php - 计算 1 列中的项目并将其分成两列

html - 转义用户通过文本字段输入的“&”符号?

javascript - 链接导致刷新

html - 使用 CSS 进行网页设计过程 - 期间还是之后?

php - 使用 Smarty 从我的 HTML 中去除 P 标签

php - 删除所有不需要的字符

php - 想让 'example.com/home'在地址栏显示为 'example.com'

php - MySQLi 查询未正确返回结果

PHP,文本区域中的 strip_tags 剥离\n。如何阻止它?