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