Page 1 of 1

Modified: Mathcing newline?

Posted: Wed Nov 08, 2006 11:18 pm
by alex.barylski
Attempt 1:

Code: Select all

preg_match_all('/<\?php(\S|.)*\?>?/', $buff, $php);
Attempt 2:

Code: Select all

preg_match_all('/<\?php([\s\S])*\?>?/', $buff, $php);
The above regex is supposed to match all the source code between <?php and optionally ?> hopefully returning an array of PHP tags inside any text document.

I can't figure out how to get the regex to match PHP tags which span multiple lines???

Also if you can see anything wrong with it (other than it ignores <script> <? or <% tags) please let me know :)

Cheers :)

Posted: Wed Nov 08, 2006 11:45 pm
by nickvd
Try

Code: Select all

preg_match_all('/<\?php(\S|.)*\?>?/s', $buff, $php);
Note the 's' modifier...

Posted: Wed Nov 08, 2006 11:47 pm
by feyd
Add the "s" pattern modifier.

FYI, your pattern is greedy. It'll return from the first opening tag to the last question mark (or closing tag, whichever comes later) if I read it right.

Posted: Thu Nov 09, 2006 9:59 am
by pickle
Check out ~d11wtq's Regex tutorial: viewtopic.php?t=33147

Posted: Sun Nov 12, 2006 2:22 pm
by GeertDD

Code: Select all

// Try:
preg_match_all('/<\?php(.*?)(?:\?>)?/s', $buff, $php);