The matching rule cannot use "/<script. * <\/script>/I", because it cannot match the line break, so multiple lines of js won't match. Use "/<script [\ s \ S] *? <\/Script>/I ". Inside? Indicates that there are as few duplicates as possible, that is, matching the nearest </script>.
Source code example:
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: text/html; charset = UTF-8 ");
$ Str = '<script type = "text/javascript" src = "dd. js"> </script>
Test php Regular Expression matching js Code test php Regular Expression matching js Code
<Script type = "text/javascript" src = "123.js"> </script>
<Script type = "text/javascript">
Var aa = "sdsds ";
Alert (aa );
</Script>
Test php Regular Expression matching js Code ';
$ Preg = "/<script [\ s \ S] *? <\/Script>/I ";
$ Newstr = preg_replace ($ preg, "", $ str, 3); // In the fourth parameter, 3 indicates to be replaced three times. The default value is-1. Replace all
Echo $ newstr;
?>