Skip to content

Instantly share code, notes, and snippets.

@parallaxisjones
Forked from beck24/script match
Created August 28, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parallaxisjones/0d222055a7defbccbdcc4fefad6af361 to your computer and use it in GitHub Desktop.
Save parallaxisjones/0d222055a7defbccbdcc4fefad6af361 to your computer and use it in GitHub Desktop.
preg_match_all <script></script> match
<?php
$html = <<<HTML
<html>
<head>
<script src="http://example.com"></script>
</head>
<body>
<div>
<script>
// inline js
$(document).ready( function() {
});
</script>
Hello World
</div>
</body>
</html>
HTML;
$regex = "/\<script(.*?)?\>(.|\\n)*?\<\/script\>/i";
preg_match_all($regex, $html, $scripts);
print_r($scripts);
/* **
Need regex such that print_r($scripts) will give me:
array(
[0] => <script src="http://example.com"></script>
[1] => <script>// inline js$(document).ready( function() {});</script>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment