homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved