Jump to content

preg_match help


moon 111

Recommended Posts

For some reason the following code is returning

Array ( ) 

 

<?php

$subject = '<div class="story">
<h5>
<a href="http://www.nytimes.com/2008/04/23/world/europe/23coal.html?hp">Europe Turns to Coal Again, Raising Alarms on Climate</a>
</h5>
<div class="byline">By ELISABETH ROSENTHAL</div>
<p class="summary">European countries plan to use coal, generally the dirtiest fuel on earth, in new power plants.</p>';

$regex = '|<div class="story">\s++<h5>\s++<a href="([^"]*)">([^<]*)</a>\s++</h5>\s++
<div class="byline">([^<]*)</div>\s++<p class="summary">
([^<]*)</p>|';

preg_match($regex, $subject, $match);

print_r($match);

?>

 

Help?

Link to comment
https://forums.phpfreaks.com/topic/102510-preg_match-help/
Share on other sites

Try a pattern like this instead:

|<div class="story">\s*<h5>\s*<a href="([^"]*)">([^<]*)</a>\s*</h5>\s*
<div class="byline">([^<]*)</div>\s*<p class="summary">
([^<]*)</p>|'

 

Your match for spaces seemed a little wonky. You probably need to use the * to allow for zero or more. It seems to work just fine this way.

Link to comment
https://forums.phpfreaks.com/topic/102510-preg_match-help/#findComment-524858
Share on other sites

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.