N-Bomb(Nerd) Posted June 14, 2009 Share Posted June 14, 2009 I have a div that I'm trying to extract content from, however I'm having a bit of trouble. This div is obviously going to have more values within the div. <div id="babyblue">lots of <b>random</b> stuff going on in here.. wanna grab it all</div> How would one go about this? Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/ Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 14, 2009 Author Share Posted June 14, 2009 Also, forgot to mention that I can't use DOM in this case.. has to be regex. Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855638 Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 preg_match($variable, '/\<div id\=\"babyblue\"\>(.*?)\<\/div\>/isS', $matches); print_r($matches); Is worth a shot... Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855639 Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 14, 2009 Author Share Posted June 14, 2009 preg_match($variable, '/\<div id\=\"babyblue\"\>(.*?)\<\/div\>/isS', $matches); print_r($matches); Is worth a shot... I'm getting an empty array output using that.. Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855650 Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 Oh man I'm such a dick, try this preg_match('/\<div id\=\"babyblue\"\>(.*?)\<\/div\>/isS', $variable, $matches); print_r($matches); I was thinking strstr() syntax...it's stupid the way they do it opposite ways. Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855653 Share on other sites More sharing options...
N-Bomb(Nerd) Posted June 14, 2009 Author Share Posted June 14, 2009 Alright, so after some investigating as to why this wasn't working.. it appears in some of the divs there's actually more divs inside.. therefore it could read a div inside of the div I'm trying to get and close early. Would using DOM actually solve my problem here? The thing is, I've never used DOM in my life and I have no idea how I would set that up. Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855659 Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 Well, an id should only be used once, hence its name... What's being returned from what I just posted? And I wouldn't know...I've never used DOM either Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855664 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 But you said earlier... Also, forgot to mention that I can't use DOM in this case.. has to be regex. anywhoo....yes, the problem is that regex does not handle nested tagging very well. More accurately, it handles it very poorly. The core of DOM is in fact regex, but the broader scope of DOM is that it goes through and creates a well, model (hence the name) of the page, as a whole. So it properly matches opening/closing tags up by looking at the document as a whole. So you can use regex, but in order to make it any kind of accurate, you're going to have to look at the content as a whole and walk through everything matching them up...which is reinventing the wheel. There is already another active thread about DOM on this very page that looks suspiciously familiar to this thread (trying to get contents of a div...asking for a regex solution, being told about DOM...hmm...not insinuating anything..just..awfully coincidental...). Anyways..it's on the front page of the forum, so look into how to do the whole DOM thing there. edit: in fact, I'll even give you a link to it, save yourself the trouble of trying to find it. http://www.phpfreaks.com/forums/index.php/topic,256532.0.html Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855666 Share on other sites More sharing options...
Michdd Posted June 14, 2009 Share Posted June 14, 2009 That's it wasn't made by the same person. I'm the person who made the other topic, and the final solution that Daniel posted worked perfectly for me. So you should try the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/162146-extracting-div-contents/#findComment-855678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.