shiny_spoon Posted May 31, 2007 Share Posted May 31, 2007 Hey all, I've always been a complete imbecile when it comes to preg functions. No matter how many tutorials I read or how many times I actually use it for simple searches, I never seem to get the hang of it. I'm at my wits end right now, so I figured I'd come on here and ask you all for some help! I'm parsing HTML files like so: $page = "http://www.location.com"; $page_contents = file_get_contents($page); preg_match_all("I have no idea what to put here! ", $page_contents, $result); I need to find the following pattern: <div class="className">(pattern goes here)</div> So basically anything within a certain div class. I've tried about two hundred possibilities and have gotten absolute nothing out of it! Hopefully one of you wise people can give me a hand! Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/53679-help-needed-with-preg_match_all/ Share on other sites More sharing options...
Caesar Posted May 31, 2007 Share Posted May 31, 2007 <?php $page_contents = file_get_contents($page); preg_match_all('/\<div class=\"[aA-zZ]+\"\>(.*)\<\/div\>/', $page_contents, $result); ?> Link to comment https://forums.phpfreaks.com/topic/53679-help-needed-with-preg_match_all/#findComment-265345 Share on other sites More sharing options...
saf Posted May 31, 2007 Share Posted May 31, 2007 I have yet to get used to using preg_match, but here's one method that I have used in the past (Note: this is with consideration that you want only one value and the specific div we are looking for is unique to the rest of the code): <?php $text = file_get_contents($page); $StartAt = "<div class\"class_name\">"; $EndAt = "</div>"; $startPos= stripos($text,$StartAt)+strlen($StartAt); $endPos= stripos($text,$EndAt,$startPos)-$startPos; $FinalVal=substr($text,$startPos,$endPos); print $FinalVal; ?> Link to comment https://forums.phpfreaks.com/topic/53679-help-needed-with-preg_match_all/#findComment-265350 Share on other sites More sharing options...
shiny_spoon Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks for the example Caesar, I managed to get it working! Oh, and thank you for the alternative, saf. I find it much easier to understand than all the craziness going on in preg's. Link to comment https://forums.phpfreaks.com/topic/53679-help-needed-with-preg_match_all/#findComment-265364 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.