自定义的 strip tags()

728x15 ad here

出自Guoshuang Wiki

跳转到: 导航, 搜索

strip_tags() 的问题很多。

in PHP has several problems. It doesn’t recognize that css within the style tags are not document text. It will not remove HTML entities or content within script tags. strip_tags() fails for invalid HTML. In short strip_tags() is not advisable to use except for trivial cases. The best solution I have come across is by uersoy at tnn dot net:

function html2txt($document){
  $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
                 '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
                 '@<[/!]*?[^<>]*?>@si',            // Strip out HTML tags
                 '@<![sS]*?--[ tnr]*>@'         // Strip multi-line comments including CDATA
  );
  $text = preg_replace($search, '', $document);
  return $text;
}
个人工具
125x125 ad here
Advertisement
120x600 ad here