| replacing all but between markers or something |
ncreegan

msg:1312004 | 1:12 am on Mar 23, 2006 (gmt 0) | 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...
|
coopster

msg:1312005 | 1:27 am on Mar 23, 2006 (gmt 0) | $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.
|
ncreegan

msg:1312006 | 12:09 pm on Mar 23, 2006 (gmt 0) | Thanks coopster, right on. Especially appreciate the explanation of the expression you used, above and beyond :)
|
|
|