php中html如何转text

发布时间:2021-05-20 11:51:58 作者:小新
来源:亿速云 阅读:237

这篇文章主要介绍php中html如何转text,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

php html转text的方法:首先创建一个PHP示例文件;然后通过“function html2text($str){...}”方法对html中的标记进行替换即可。

本文操作环境:Windows7系统、PHP7.1版,DELL G3电脑

php将HTML转换为txt文本的函数

利用php的preg_replace函数对html中的标记进行替换。

代码如:

 function html2text($str){  
  $str = preg_replace("/<style .*?<\/style>/is", "", $str);  $str = preg_replace("/<script .*?<\/script>/is", "", $str);  
 $str = preg_replace("/<br \s*\/?\/>/i", "\n", $str);  
 $str = preg_replace("/<\/?p>/i", "\n\n", $str);  
 $str = preg_replace("/<\/?td>/i", "\n", $str);  
 $str = preg_replace("/<\/?div>/i", "\n", $str);  
 $str = preg_replace("/<\/?blockquote>/i", "\n", $str);  
 $str = preg_replace("/<\/?li>/i", "\n", $str);  
 $str = preg_replace("/\&nbsp\;/i", " ", $str);  
 $str = preg_replace("/\&nbsp/i", " ", $str);  
 $str = preg_replace("/\&amp\;/i", "&", $str);  
 $str = preg_replace("/\&amp/i", "&", $str);    
 $str = preg_replace("/\&lt\;/i", "<", $str);  
 $str = preg_replace("/\&lt/i", "<", $str);  
 $str = preg_replace("/\&ldquo\;/i", '"', $str);  
 $str = preg_replace("/\&ldquo/i", '"', $str);  
 $str = preg_replace("/\&lsquo\;/i", "'", $str);  
 $str = preg_replace("/\&lsquo/i", "'", $str);  
 $str = preg_replace("/\&rsquo\;/i", "'", $str);  
 $str = preg_replace("/\&rsquo/i", "'", $str);  
 $str = preg_replace("/\&gt\;/i", ">", $str);   
 $str = preg_replace("/\&gt/i", ">", $str);   
 $str = preg_replace("/\&rdquo\;/i", '"', $str);   
 $str = preg_replace("/\&rdquo/i", '"', $str);   
 $str = strip_tags($str);  
 $str = html_entity_decode($str, ENT_QUOTES, $encode);  
 $str = preg_replace("/\&\#.*?\;/i", "", $str);          
 
 return $str;
 }

php是什么语言

php,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写。PHP 是一种 HTML 内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。

以上是“php中html如何转text”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. text/html和text/plain的区别
  2. 如何解决php html转ubb问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php html text

上一篇:php如何删除开头字符串

下一篇:windows出现无法显示图片可能内存不足的解决方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》