0

I am working on templating system. I do have input of html document from user which contains html tags and javascript codes.

i am fine with html tags. I would only like to match all the javascript codes and add it to array for loading it at the bottom of page.

i tried finding script tag content using preg_match bot not working. my code:

    $strPattern = "/<script>[^(<\/script>)]<\/script>/i";
    preg_match_all( $strPattern, $strReturn, $arrMatches);
    echo 'matches---';
    var_dump( $arrMatches );
    $strReturn = preg_replace($strPattern, '', $strReturn);

What am i missing?

KoolKabin
  • 17,157
  • 35
  • 107
  • 145

1 Answers1

0

Use PHP DOMDOCUMENT, http://php.net/manual/en/class.domdocument.php

You can traverse and process the DOM tree and format it accordingly. That would make your life easier compared to REGEX.

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • its nice and i even tried too. it worked but can't replace/clear the script tag from source string – KoolKabin Mar 28 '12 at 09:31
  • you can read the node, then delete it as mentioned here : http://stackoverflow.com/questions/1171597/how-to-remove-an-html-element-using-the-domdocument-class , then append it at the bottom of the document – DhruvPathak Mar 28 '12 at 12:43
  • it adds head, body and doctype too if they aren't there... i dont want them – KoolKabin Mar 31 '12 at 09:51