Welcome to WebmasterWorld Guest from 126.96.36.199
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...
$pattern = "/<div[^>]*>(.*)<\/div>/Uis";
preg_match_all($pattern, $string, $matches);
print '<pre>'; print_r($matches); exit('</pre>');
$matchesshould 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.