Modified: Mathcing newline?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Modified: Mathcing newline?

Post 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 :)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Try

Code: Select all

preg_match_all('/<\?php(\S|.)*\?>?/s', $buff, $php);
Note the 's' modifier...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Check out ~d11wtq's Regex tutorial: viewtopic.php?t=33147
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

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