citricsquid Posted September 20, 2009 Share Posted September 20, 2009 Hi, I've been using explode to split the contents of a page for a while now, if I wanted to find "<span class="hello">hey there!</span>" on a page I'd just do "explode('<span class="hello">hey there!</span>', $page);" however now I'm in a position where "<span class="hello">hey there!</span>" appears multiple times on a page and I need to retrieve each one. How would I go about doing this for multiple things? I'll give an example of the output I want to check: <div class="something">Hi there!</div> <div class="something">Hello!</div> <div class="something">hi i am</div> <div class="something">rofl</div> <div class="something">dsfssdf</div> and I want to get the contents of each <div class="something">. I assume this will require more than just using explode, regex maybe? I know how to get a single one using explode, but not multiple. So the final result would be an array like: $somethings[0] = "Hi there!"; $somethings[1] = "Hello!; $somethings[2] = "hi i am"; any ideas? Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/ Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 Like this <?php $HTML = '<div class="something">Hi there!</div> <div class="something">Hello!</div> <div class="something">hi i am</div> <div class="something">rofl</div> <div class="something">dsfssdf</div>'; preg_match_all('%<div class="[^"]*">([^<]*)</div>%sm', $HTML, $somethings); $somethings = $somethings[1]; var_dump($somethings); ?> array(5) { [0]=> string(9) "Hi there!" [1]=> string(6) "Hello!" [2]=> string(7) "hi i am" [3]=> string(4) "rofl" [4]=> string(7) "dsfssdf" } Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921844 Share on other sites More sharing options...
citricsquid Posted September 20, 2009 Author Share Posted September 20, 2009 This works perfectly, however if I want to go "more advanced" I'm sort of lost: If I wanted to find EVERYTHING between <div class="something">{HERE}</div> how would I do it? Let's say, for example, I have: <div class="something"><div id="rofl">dfssdffds</div><img src="lol.jpg"><a href="#">dfsffs</a></div> and I want to match between <div class="something"></div> So I want it to return: <div id="rofl">dfssdffds</div><img src="lol.jpg"><a href="#">dfsffs</a> How would I match that? I tried playing around with it but I'm lost, any ideas? Thanks for the help so far Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921856 Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 change preg_match_all('%<div class="[^"]*">([^<]*)</div>%sm', $HTML, $somethings); to preg_match_all('%<div class="[^"]*">(.*?)</div>%sim', $HTML, $somethings); should do it, (untested) Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921869 Share on other sites More sharing options...
citricsquid Posted September 20, 2009 Author Share Posted September 20, 2009 works beautifully, thanks! I was missing out the ? before, regex is confusing. Thanks ever so much for the help, solved and appreciated! Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921872 Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 Welcome, RegEx gets easier with time, I'm teaching a guy at work, his slower getting to grips with it, but the first time i showed him.. it said "F*** that!" but when i updates his 20someting lines of broken code with 3 lines of working code.. he started pay attention. Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921878 Share on other sites More sharing options...
citricsquid Posted September 20, 2009 Author Share Posted September 20, 2009 Welcome, RegEx gets easier with time, I'm teaching a guy at work, his slower getting to grips with it, but the first time i showed him.. it said "F*** that!" but when i updates his 20someting lines of broken code with 3 lines of working code.. he started pay attention. heh, yeah, I'm a sort of "hmm, that does look very hard but I guess it'll save me time..." then end up quitting because it's too complicated for my non-programmer brain to handle on that note, can't get adding new lines to work now. by default . ignores new lines, but mine has new lines and adding /s (the modifier) doesn't seem to work. Damn you regex! Link to comment https://forums.phpfreaks.com/topic/174924-solved-find-all-elements-that-match-on-a-page/#findComment-921885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.