最近做一个问题,去除html中代码的JS脚本,代码如下
1 <?php 2 $text = <<<EOF 3 ilove you 4 <scrip></sss> 5 <script> ok </script> 6 kkyz <dd>m,dfd 7 <script> 8 sdfsdf 9 </script> 10 <script> no </script> 11 ook 12 EOF; 13 14 echo "orginal text: \n\n" . $text . "\n\n"; 15 $pattern1 = "@<script.*?>(.*?)</script>@is"; #等效于"@<script.*>(.*)</script>@isU" 16 #末尾的i表示忽略大小写,s表示.可以匹配换行符,U与?等效,表示关闭无限匹配 17 $pattern2 = "@<script.*>(.*)</script>@is"; #这是无限匹配的pattern,php默认就是无限匹配 18 $text1 = preg_replace($pattern1, "", $text); 19 $text2 = preg_replace($pattern2, "", $text); 20 echo "text1: \n\n" . $text1 . "\n\n"; 21 echo "text2: \n\n" . $text2 . "\n\n"; 22 ?>
结果如下: