Jump to content

preg_match more than the first instance


markvaughn2006

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.