• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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

1 Solution

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

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now