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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.