$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖
分享到:




[PHP 语言] 分享php正则删除/去除/过滤html标记代码

php正则怎么去除js代码
2 ~& w! z8 _) b0 [
6 R9 t$ ?2 p4 J! T; O
  1. <?php
  2. $str = 'yes,<scRipt type="text/javascript">
  3. var IKFromImport=false;
  4. </script>ok!';
  5. $text = preg_replace("'<script(.*?)<\/script>'is","",$str);
  6. //yes,ok!
  7. echo $text;
复制代码
& L# Y( _4 k* s

" k8 T( M2 R- ~/ d6 p% }php 正则匹配css$ G6 h$ s9 R/ q# M) D4 b$ r1 |

7 p; S* b( b+ m1 s0 X
- w! \! b$ P& Z0 {1 ~; c6 T5 b
  1. echo preg_replace('/(?:(<link.+href=\")((?!http).+\.css){1}(\".*(?:type=\"text\/css\"){1}.*>))|(?:(<link.+(?:type=\"text\/css\"){1}.*href=\")((?!http).+\.css){1}(\".*>))/i',"\${1}\${4}http://a.com/\${2}\${5}\${3}\${6}",$html);
复制代码

; V& z% u# d, q+ d% s0 P( p+ j. @4 }8 s% D2 }( y
  1. $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
  2. "'<style[^>]*?>.*?</style>'si", // 去掉 css
  3. "'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记
  4. "'<!--[/!]*?[^<>]*?>'si", // 去掉 注释 标记
  5. "'([rn])[s]+'", // 去掉空白字符
  6. "'&(quot|#34);'i", // 替换 HTML 实体
  7. "'&(amp|#38);'i",
  8. "'&(lt|#60);'i",
  9. "'&(gt|#62);'i",
  10. "'&(nbsp|#160);'i",
  11. "'&(iexcl|#161);'i",
  12. "'&(cent|#162);'i",
  13. "'&(pound|#163);'i",
  14. "'&(copy|#169);'i",
  15. "'&#(d+);'e"); // 作为 PHP 代码运行

  16. $replace = array ("",
  17. "",
  18. "",
  19. "",
  20. "\1",
  21. "\"",
  22. "&",
  23. "<",
  24. ">",
  25. " ",
  26. chr(161),
  27. chr(162),
  28. chr(163),
  29. chr(169),
  30. "chr(\1)");
  31. //$document为需要处理字符串,如果来源为文件可以$document = file_get_contents($filename);
  32. $out = preg_replace($search, $replace, $document);
复制代码
! d3 a, N  K1 a
, v) U$ z- |; B! B" g( P+ D7 o1 B
也可以使用php的内置函数strip_tags()清除html,js,注释等标记




返回列表
Namecheap
Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting
互联网安全