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




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

php正则怎么去除js代码* [) ~, G/ Z1 |& J. L: J) `

) K0 \' z) }' n( [$ o  s
  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;
复制代码

- m( W* U, O5 t$ D
9 R- @$ C* c6 {' K1 B' ~php 正则匹配css: \* Z) w1 ?# C6 @3 X" r8 L

4 u" C* F8 [7 R' Q7 |0 \2 B6 v4 A
+ N! {! u7 S4 i" g+ L
  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);
复制代码
) M% \/ g* a  U2 U

! {1 P! s- K0 R- p" `" D) i' e1 O5 ~1 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);
复制代码

) }4 ?0 o7 o9 u5 L1 I; G7 [9 p% Y& t2 b+ V
也可以使用php的内置函数strip_tags()清除html,js,注释等标记

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