Regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
anon404
Forum Newbie
Posts: 12
Joined: Wed Dec 23, 2009 4:09 am

Regular expression

Post 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
Last edited by anon404 on Tue Feb 02, 2010 9:29 pm, edited 2 times in total.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regular expression

Post by ridgerunner »

Edit: see next post
Last edited by ridgerunner on Wed Feb 03, 2010 1:30 pm, edited 1 time in total.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regular expression

Post 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 (.*?)
Last edited by ridgerunner on Thu Feb 11, 2010 12:29 pm, edited 1 time in total.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Regular expression

Post by klevis miho »

I always use this:

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