首页 > php教程 > PHP源码 > 正文

strip_tags延伸函数-处理html(采集用的上)

PHP中文网
发布: 2016-05-25 17:07:04
原创
1715人浏览过

strip_tags延伸函数-处理html(采集用的上)

/** 
 * This function turns HTML into text  * 将html转化为txt   
*/ 
function html2txt($document) {     
$search = array ('@<script[^>]*?>.*?</script>@si', 
// Strip out javascript                     
'@<style[^>]*?>.*?</style>@siU', 
// Strip style tags properly                     
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags                     
'@<![\s\S]*?--[ \t\n\r]*>@' )// Strip multi-line comments including CDATA     ;     
$text = preg_replace ( $search, '', $document );     
return $text; }  
/**  
* removes the HTML tags along with their contents  
* 移除/过滤html标签并且移除标签内的内容  
* 注:$tags为需要保留的标签  invert为true时结果相反  
*/ 
function strip_tags_content($text, $tags = '', $invert = FALSE) {    
 preg_match_all ( '/<(.+?)[\s]*\/?[\s]*>/si', trim ( $tags ), $tags );     
 $tags = array_unique ( $tags [1] );           
 if (is_array ( $tags ) and count ( $tags ) > 0) {         
 if ($invert == FALSE) {             
 return preg_replace ( '@<(?!(?:' . implode ( '|', $tags ) . ')\b)(\w+)\b.*?>.*?</\1>@si', '', $text );       
   } else {             
   return preg_replace ( '@<(' . implode ( '|', $tags ) . ')\b.*?>.*?</\1>@si', '', $text );         }    
    } elseif ($invert == FALSE) {        
     return preg_replace ( '@<(\w+)\b.*?>.*?</\1>@si', '', $text );    
      }     
     return $text; 
     }
登录后复制

 以上就是strip_tags延伸函数-处理html(采集用的上)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Devv
Devv

Devv是一个专为程序员打造的新一代AI搜索引擎

Devv 140
查看详情 Devv
相关标签:
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号