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




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

php正则怎么去除js代码" F( l: S9 n+ P' N4 f9 H2 l2 }, d
" s, u1 t  G+ E: s. W& R! K; |+ C1 }
  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;
复制代码

' V1 o0 r7 ?9 i* |3 N' ]5 A1 a7 [# I- c/ f" x9 I- X; T
php 正则匹配css
" K& D- t7 ~' ~% A
2 A7 w" x5 ?5 c+ J5 D8 w
5 X( w0 U) s# B% i1 }
  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);
复制代码
0 K/ z- n4 ~* o6 F0 F5 n
: n  {' a+ U9 l, \- a
  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);
复制代码
0 J+ v( ?0 l" p5 I- B' L& x6 {

; z4 @) I+ }" [3 b4 W! j也可以使用php的内置函数strip_tags()清除html,js,注释等标记

美国VPS、域名代购:http://tu8l.taobao.com

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