php uses regular filtering for js script code instances

  • 2021-06-28 08:49:00
  • OfStack

Matching rules cannot use'/ < script.* < \/script > /i ", because it cannot match to a line break, so many lines of js cannot be matched off.To use'/ < script[\s\S]*? < \/script > /i'.Inside?Repeats as little as possible, that is, matches the nearest one < /script > .

Source Example:


<?php

 header("Content-type:text/html;charset=utf-8");
 $str = '<script type="text/javascript" src="dd.js"></script>
 test php Regular Match Out js Code Testing php Regular Match Out js Code Testing php Regular Match Out js Code Testing php Regular Match Out js Code Testing php Regular Match Out js Code Testing php Regular Match Out js Code 
<script type="text/javascript" src="123.js"></script>
<script type="text/javascript">
 var aa = "sdsds";
 alert(aa);
</script>
 test php Regular Match Out js Code ';
 $preg = "/<script[\s\S]*?<\/script>/i";
 $newstr = preg_replace($preg,"",$str,3);    // No. 4 Of the parameters 3 Representation Replacement 3 Secondary, default is -1 , replace all 
 echo $newstr;
?>


Related articles: