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




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

php正则怎么去除js代码& V( `2 x( u6 x0 ]2 A

4 n7 J) W% S- u3 ~' m8 T' e3 p
  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;
复制代码
! z# t5 q( l9 g" y& J. ^; f' x5 e$ y' h
8 z( y3 o. [9 F  z7 T
php 正则匹配css
% |& R8 Z" w8 u" V, F) T& y9 z4 F3 y- q. C! r9 V) W
0 i6 l' [- r6 ?0 E  C" \
  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);
复制代码

6 S5 i8 c! i$ W& L* u! T$ P& v9 j( `% }1 T* B- C4 p# {4 k9 [
  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);
复制代码
; j: |$ j2 ^; Q0 ~' c
7 t/ y3 f, M: y4 f$ |$ J5 l
也可以使用php的内置函数strip_tags()清除html,js,注释等标记




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