Skip to content

Instantly share code, notes, and snippets.

@beck24
Created November 27, 2012 23:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save beck24/4157984 to your computer and use it in GitHub Desktop.
Save beck24/4157984 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>
)
@AndikaTanpaH
Copy link

AndikaTanpaH commented Apr 27, 2018

i use this for this regex example

preg_match_all('/\<script(.*?)?\>(.|\s)*?\<\/script\>/i', $html, $scripts);
print_r($scripts[0]);

===========

\s

regex for all white space enter, tab

$scripts[0]

will give you all fullmatch only (not group etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment