源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-12-21 12:46 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php实现转换html格式为文本格式的方法
本文实例讲述了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;
}

[b]PS:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版: [/b]  [b]php代码在线格式化美化工具:[/b][url=http://tools.jb51.net/code/phpformat]http://tools.jb51.net/code/phpformat[/url] [b]对于代码及编码转换感兴趣的朋友还可参考本站在线工具:[/b] [b]在线编码转换工具(utf-8/utf-32/Punycode/Base64):[/b] [url=http://tools.jb51.net/transcoding/decode_encode_tool]http://tools.jb51.net/transcoding/decode_encode_tool[/url] [b]在线UBB/HTML代码转换工具:[/b] [url=http://tools.jb51.net/transcoding/ubb2html]http://tools.jb51.net/transcoding/ubb2html[/url] [b]中文繁体字简体字转换(繁简转换)工具:[/b] [url=http://tools.jb51.net/transcoding/convertzh]http://tools.jb51.net/transcoding/convertzh[/url] [b]在线图片转换BASE64工具:[/b] [url=http://tools.jb51.net/transcoding/img2base64]http://tools.jb51.net/transcoding/img2base64[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/150.htm]php排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/242.htm]PHP常用遍历算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部