Home  >  Article  >  php教程  >  php使用正则过滤js脚本代码实例

php使用正则过滤js脚本代码实例

WBOY
WBOYOriginal
2016-06-13 09:35:21769browse

匹配的规则不能用 "//i",因为它不能匹配到换行符,那么多行js就匹配不掉了。要用 "/<script>/i"。里面的?表示尽可能少重复,也就是匹配最近的一个</script>。

源码范例:

复制代码 代码如下:



 header("Content-type:text/html;charset=utf-8");
 $str = '
测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码


测试php正则匹配掉js代码';

 $preg = "/<script>/i";<BR> $newstr = preg_replace($preg,"",$str,3); //第四个参数中的3表示替换3次,默认是-1,替换全部<BR> echo $newstr;</script>

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn