当前位置:Gxlcms > PHP教程 > php html如何转换 text

php html如何转换 text

时间:2021-07-01 10:21:17 帮助过:29人阅读

php html转换text的方法:首先创建一个PHP示例文件;然后定义一个html2text方法;最后通过“preg_replace”实现格式匹配即可。

推荐:《PHP视频教程》

php html转换 text

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

  1. function html2text($str){
  2. $str = preg_replace("/<style .*?<\\/style>/is", "", $str);
  3. $str = preg_replace("/<script .*?<\\/script>/is", "", $str);
  4. $str = preg_replace("/<br \\s*\\/>/i", ">>>>", $str);
  5. $str = preg_replace("/<\\/?p>/i", ">>>>", $str);
  6. $str = preg_replace("/<\\/?td>/i", "", $str);
  7. $str = preg_replace("/<\\/?div>/i", ">>>>", $str);
  8. $str = preg_replace("/<\\/?blockquote>/i", "", $str);
  9. $str = preg_replace("/<\\/?li>/i", ">>>>", $str);
  10. $str = preg_replace("/ /i", " ", $str);
  11. $str = preg_replace("/ /i", " ", $str);
  12. $str = preg_replace("/&/i", "&", $str);
  13. $str = preg_replace("/&/i", "&", $str);
  14. $str = preg_replace("/</i", "<", $str);
  15. $str = preg_replace("/</i", "<", $str);
  16. $str = preg_replace("/“/i", '"', $str);
  17. $str = preg_replace("/&ldquo/i", '"', $str);
  18. $str = preg_replace("/‘/i", "'", $str);
  19. $str = preg_replace("/&lsquo/i", "'", $str);
  20. $str = preg_replace("/'/i", "'", $str);
  21. $str = preg_replace("/&rsquo/i", "'", $str);
  22. $str = preg_replace("/>/i", ">", $str);
  23. $str = preg_replace("/>/i", ">", $str);
  24. $str = preg_replace("/”/i", '"', $str);
  25. $str = preg_replace("/&rdquo/i", '"', $str);
  26. $str = strip_tags($str);
  27. $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
  28. $str = preg_replace("/&#.*?;/i", "", $str);
  29. return $str;
  30. }

以上就是php html如何转换 text的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行