Page 1 of 1

Regular expression

Posted: Mon Feb 01, 2010 5:39 pm
by anon404
Moved by moderator to Regex forum:

Why is preg_match_all getting empty results for this html:
<div class="info">
<h5>Plot:</h5>
<div class="info-content">
A lawman apprehends a notorious outlaw and gives him 9 days to kill his older brother, or else they'll execute his younger brother. <a class="tn15more inline" href="/title/tt0421238/plotsummary" onClick="(new Image()).src='/rg/title-tease/plotsummary/images/b.gif?link=/title/tt0421238/plotsummary';">full summary</a>

Code: Select all

function get_movie_info($html)
  {
    preg_match_all('/<h5>Plot:<\/h5>(.*)<a/', $html, $result);
    if(isset($result[0])) {
      foreach($result[0] as $val) {
        echo $val;
      }
    }else {
      echo "result[0] not set<br/>";
    }
    if(isset($result[1])) {
      foreach($result[1] as $val) {
        echo $val;
      }
    }else {
      echo "result[1] not set<br/>";
    }
  }
Edit: is

Re: Regular expression

Posted: Tue Feb 02, 2010 4:48 pm
by ridgerunner
Edit: see next post

Re: Regular expression

Posted: Wed Feb 03, 2010 1:30 pm
by ridgerunner
Ok, now I can see the problem. Your regex uses a dot star .* but you have forgotten to use the dot-matches-newline "single-line" 's' option. This should fix it...

Code: Select all

function get_movie_info($html)
  {
    preg_match_all('/<h5>Plot:<\/h5>(.*?)<a/s', $html, $result);
    if(isset($result[0])) {
...
Hope this helps! :)

Edit: 2010-02-11 - Klevis' version below is correct. Changed the (.*) to the lazy version (.*?)

Re: Regular expression

Posted: Thu Feb 11, 2010 5:46 am
by klevis miho
I always use this:

preg_match_all('#<h5>Plot:<\/h5>(.+?)<a#s', $html, $result);
print_r($result);