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
https://forums.phpfreaks.com/topic/162457-solved-preg_match_all-problem/
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];

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 :)

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.