php实现转换html格式为文本格式的方法

作者:cclehui 时间:2024-06-07 15:44:57 

本文实例讲述了php实现转换html格式为文本格式的方法。分享给大家供大家参考,具体如下:

有时候需要转换html格式的字符串为文本,但又需要保持一定的格式,比如要求段落变成的分段格式就可以用下面这个函数


function html2text($str){
$str = preg_replace("/<style .*?<\\/style>/is", "", $str);
$str = preg_replace("/<script .*?<\\/script>/is", "", $str);
$str = preg_replace("/<br \\s*\\/>/i", ">>>>", $str);
$str = preg_replace("/<\\/?p>/i", ">>>>", $str);
$str = preg_replace("/<\\/?td>/i", "", $str);
$str = preg_replace("/<\\/?div>/i", ">>>>", $str);
$str = preg_replace("/<\\/?blockquote>/i", "", $str);
$str = preg_replace("/<\\/?li>/i", ">>>>", $str);
$str = preg_replace("/ /i", " ", $str);
$str = preg_replace("/ /i", " ", $str);
$str = preg_replace("/&/i", "&", $str);
$str = preg_replace("/&/i", "&", $str);
$str = preg_replace("/</i", "<", $str);
$str = preg_replace("/</i", "<", $str);
$str = preg_replace("/“/i", '"', $str);
$str = preg_replace("/&ldquo/i", '"', $str);
$str = preg_replace("/‘/i", "'", $str);
$str = preg_replace("/&lsquo/i", "'", $str);
$str = preg_replace("/'/i", "'", $str);
$str = preg_replace("/&rsquo/i", "'", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/”/i", '"', $str);
$str = preg_replace("/&rdquo/i", '"', $str);
$str = strip_tags($str);
$str = html_entity_decode($str, ENT_QUOTES, "utf-8");
$str = preg_replace("/&#.*?;/i", "", $str);
return $str;
}

希望本文所述对大家PHP程序设计有所帮助。

标签:php,转换,html,文本
0
投稿

猜你喜欢

  • 查看django版本的方法分享

    2023-05-02 22:22:32
  • Python获取网页上图片下载地址的方法

    2021-01-22 13:15:26
  • 12个Pandas/NumPy中的加速函数使用总结

    2022-10-27 13:52:25
  • python实现Adapter模式实例代码

    2021-10-20 00:43:02
  • Python中bisect的用法

    2023-11-05 08:47:53
  • 用Python登录好友QQ空间点赞的示例代码

    2023-08-08 09:29:40
  • 用ASP对网页进行限制性的访问

    2008-07-03 13:02:00
  • OpenCV视频流Python多线程处理方法详细分析

    2022-02-14 20:29:45
  • 垂直栅格与渐进式行距(下)

    2009-07-09 16:52:00
  • 基于Python实现的购物商城管理系统

    2021-08-27 15:06:43
  • python模块导入的细节详解

    2021-01-09 19:18:08
  • 详解Python遍历字典的键和值

    2021-07-19 23:02:14
  • vue实现二维码扫码功能(带样式)

    2024-04-10 10:31:39
  • Pycharm之快速定位到某行快捷键的方法

    2022-06-20 14:07:53
  • MySQL动态SQL拼接实例详解

    2024-01-20 15:13:55
  • 使用TensorFlow直接获取处理MNIST数据方式

    2022-12-16 22:35:49
  • 检查并修复mysql数据库表的具体方法

    2024-01-24 13:39:43
  • python调用matlab的m自定义函数方法

    2023-11-11 21:40:52
  • 多种方式实现js图片预览

    2024-04-23 09:24:44
  • Python内置方法实现字符串的秘钥加解密(推荐)

    2023-10-22 23:45:35
  • asp之家 网络编程 m.aspxhome.com