Jump to content

[SOLVED] Preg_Match_All Problem


N-Bomb(Nerd)

Recommended Posts

I'm currently using this preg function:

preg_match_all('/<div class="vlc east coast">(.*)<\/div>/i', $Page, $Content);

 

However, it doesn't seem to give me the output of what's inside of that div. The actual div looks like this:

<div class="vlc east coast">
Visit Us:<br><img src='visit.jpg' width='250' height='250 alt='Visit Us' />
</div>

 

What am I doing wrong?

 

Link to comment
Share on other sites

Probably because of the white space. Try this:

 

preg_match_all("/<div class=\"vlc east coast\">((.|\s)*)<\/div>/i", $Page, $Content)

 

This works:

$str = '<div class="vlc east coast">
Visit Us:<br><img src=\'visit.jpg\' width=\'250\' height=\'250\' alt=\'Visit Us\' />
</div>';
$pattern = "/<div class=\"vlc east coast\">((.|\s)*)<\/div>/i";
if(preg_match_all($pattern, $str, $matches)) echo $matches[1][0];

Link to comment
Share on other sites

The dot doesn't match new lines by default; you'll have to add the s modifier (after the last slash, next to the i modifier). You should also make the star lazy, by adding a question mark right after it. More efficient.

 

And know that a nested div will stop the match. But if there aren't any, you're fine :)

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.