PHP RegEx - Contents of all script tags

Posted on 2009-04-26
Last Modified: 2012-08-13

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

Question by:Mr_Splash
    1 Comment
    LVL 13

    Accepted Solution


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

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now