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

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

PHPz
Release: 2018-10-09 14:49:25
forward
1293 people have browsed it

匹配的规则不能用 "//i",因为它不能匹配到换行符,那么多行js就匹配不掉了。

要用 "//i"。里面的?表示尽可能少重复,也就是匹配最近的一个。

源码范例:

代码如下:

<?php
 header("Content-type:text/html;charset=utf-8");
 $str = &#39;<script type="text/javascript" src="dd.js"></script>
测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码测试php正则匹配掉js代码
<script type="text/javascript" src="123.js"></script>
<script type="text/javascript">
 var aa = "sdsds";
 alert(aa);
</script>
测试php正则匹配掉js代码&#39;;

 $preg = "/<script[\s\S]*?<\/script>/i";
 $newstr = preg_replace($preg,"",$str,3);    //第四个参数中的3表示替换3次,默认是-1,替换全部
 echo $newstr;
?>
Copy after login

 更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
source:cnblogs.com
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