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




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

php正则怎么去除js代码
0 ^* f0 U/ b1 y4 W3 w; L0 U7 D- u' I! ]3 @6 }: T
  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;
复制代码
7 H5 N6 f6 `& ]$ Z  P9 V" S" G* A

! @9 A8 @! {. y/ `php 正则匹配css
! P7 x2 E: [. {) G
6 w$ R& T2 Q0 K0 p" v3 y4 [# i
+ R8 _% ~$ a& g
  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);
复制代码

+ R/ o9 r) L0 Z$ f  m0 {$ ~
' w7 h5 ?' O+ t8 x" L) M
  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);
复制代码

3 @" j4 }/ Y% \% _2 X$ S; Q: ?0 @- d0 n) g; a: K
也可以使用php的内置函数strip_tags()清除html,js,注释等标记

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