Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

replacing all but between markers

or something

   
1:12 am on Mar 23, 2006 (gmt 0)

10+ Year Member



My brain just isn't working the same since my portrait mode monitor died :(

I have a string like

blah blah blah blah <div id="var">text</div> blah blah blah <div id="morevar">text2</div> blah blah blah...

and I need to grab "text." It's working fine (though terribly convoluded) to knock out all the outside text, then strip "text" from remainder of the divs. I know there's a better way to accomplish this in like 2 lines, where I'm using like 15.

I'm sure preg_match_all is the answer but I haven't used it and am not familiar with the array structure.

Thanks in advance...

1:27 am on Mar 23, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$pattern = "/<div[^>]*>(.*)<\/div>/Uis"; 
preg_match_all($pattern, $string, $matches);
print '<pre>'; print_r($matches[1]); exit('</pre>');

$matches[1]
should have what you are looking for ... unless I misunderstood you. The regex says to find anything that starts with '<div', followed by zero or more of anything that doesn't close the opening div element ([^>]* -- such as an 'id' or 'class', etc), followed by the close of this opening div '>', followed by anything, and I mean anything, including more tags like <span> or anything else, followed by a closing '</div>'.

The U is ungreedy, meaning stop at the first closing '</div>' we find.
The i makes it case-insensitive.
The s says that the dot metacharacter in the pattern matches all characters, including newlines.

12:09 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



Thanks coopster, right on.

Especially appreciate the explanation of the expression you used, above and beyond :)