Re: [PHP] Simple RegEx to pull out content between 2 markers



2008. 03. 29, szombat keltezéssel 11.26-kor Jon Bennett ezt írta:
Hi,

I need to grab the what's between 2 markers in a Textile / html
string, but my regex skills aren't all that hot. The text I have is:

<OPEN_MAIN_COLUMN>

h3. Article Content

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

p(image). !/app/image/1/large! Caption

p(image). !/app/image/24/large! drunken jonty

<CLOSE_MAIN_COLUMN>

<OPEN_LEFT_COLUMN>

h3. Article Content

!/app/image/7/thumb! Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.

<CLOSE_LEFT_COLUMN>

<OPEN_RIGHT_COLUMN>

h3. Article Content

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
* Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur.
* Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.

<CLOSE_RIGHT_COLUMN>

I'd like to grab the contents of left, right and main individually so
I can place them in separate form fields.

Any help greatly appreciated. I'm certain this is a cinch for people
who use regex often.

preg_match('/<OPEN_MAIN_COLUMN>(.*)<CLOSE_MAIN_COLUMN>/sUi', $string,
$matches);
echo $matches[1];

it's not very complicated, the only interesting bits are the s and U
modifiers at the end of the pattern. s makes the dot (.) to match
newlines too, so the matched text can be more than one line, the U makes
the engine 'ungreedy' which means that it stops after the first match.
this means that if you have a string like <open><close><open><close> and
match for <open>(.*)<close>, without the U modifier you would get the
part between the first <open> and the last <close>. with the modifier
you get the parts between any <open> and the next <close>

more info here:
http://hu.php.net/manual/en/reference.pcre.pattern.modifiers.php
http://hu.php.net/manual/en/reference.pcre.pattern.syntax.php

greets,
Zoltán Németh


Thanks,

Jon


--

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett


.



Relevant Pages