php转换html格式为文本格式
2013-05-04 15:55
309 查看
有时候需要转换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实现转换html格式为文本格式的方法
- php实现转换html格式为文本格式的方法
- php实现转换html格式为文本格式的方法
- Atitit.jsou html转换纯文本 java c# php
- php将HTML转换为txt文本的函数
- Atitit.jsou html转换纯文本 java c# php
- php导出csv格式数据并将数字转换成文本的思路以及代码分享
- php如何将html中的br换行符转换为文本输入中的换行符
- PHP转换文本框内容为HTML格式的方法
- Java把Html格式文本转换成纯文本的问题
- 将文本格式的文章转换为html/xml格式文本的功能封装到Javabean
- PHP将HTML转换成各种格式图片或PDF
- PHP转换文本框内容为HTML格式的方法
- PHP 转换PDF、TXT、HTML以及图像等格式的方法
- PHP将HTML转换成文本的实现代码
- php中将html中的br换行符转换为文本输入中的换行符
- php导出csv格式数据,以及将数字转换成文本解决办法
- php导出csv格式数据并将数字转换成文本的思路以及代码分享
- PHP将HTML转换成文本的实现代码