markvaughn2006 Posted May 7, 2010 Share Posted May 7, 2010 Please help me, I know this should be simple... I need to capture everything between the <example> tags on a page and can only capture the first instance of it. here's my code (exampled out) $string = file_get_contents("http://www.example.com"); preg_match('~<example>(.*)</example>~isU', $string, $example); $example1 = $example[1]; $example1 will return the first instance of what I'm looking for. I know that preg_match_all comes into play somewhere here but not sure how to make it work... what is wrong with this.. preg_match_all('~<example>(.*)</example>~isU', $string, $example); $example1 = $example[1]; $example2 = $example[2]; $example3 = $example[3]; etc... Thanks for any help!! Link to comment https://forums.phpfreaks.com/topic/201031-preg_match-more-than-the-first-instance/ Share on other sites More sharing options...
Hybride Posted May 7, 2010 Share Posted May 7, 2010 Arrays start at 0, not at 1. So your $example1 = $example[1]; is actually $example1 = $example[0]; I would do a print_r of the $example to see if anything is actually being found by the preg_match_all to begin with. Link to comment https://forums.phpfreaks.com/topic/201031-preg_match-more-than-the-first-instance/#findComment-1054786 Share on other sites More sharing options...
cags Posted May 7, 2010 Share Posted May 7, 2010 Using print_r is fairly sensible advice, but you were correct to use $example[1] not $example[0]. What you actually need to do is run preg_match_all and $example[1] will be an array of the results so $example[1][0] is the first $example[1][1] the second, etc. Link to comment https://forums.phpfreaks.com/topic/201031-preg_match-more-than-the-first-instance/#findComment-1054806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.