PHP RegEx - Contents of all script tags


I have the entire contents of a page in a string, I need to search the file for <script *>*</script> possibly using the regex below. I then want to put the matched JavaScript (without the script tags) into a new string and remove the script tags and contents from the main page contents string.

So the result is two strings; My original string with all the script tags with their contents removed and a string that contains just the contents of all the script tags.

Open in new window

Who is Participating?

preg_match('/<script[^>]*>([^<]*)<\/script[^>]*>/i', $str, $match);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.