Home > Backend Development > PHP Tutorial > php使用正则过滤js脚本代码实例_PHP

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

WBOY
Release: 2016-06-01 11:53:18
Original
791 people have browsed it

匹配的规则不能用 "//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>

?>

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template