Quy
Quy

Reputation: 29

php extract IMDb search

I need help to extract the photo urls with the string below

<tr class="findResult odd">
              <td class="primary_photo"><a href="/title/tt0499549/?ref_=fn_tt_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX32_CR0,0,32,44_AL_.jpg" /></a></td>
              <td class="result_text"><a href="/title/tt0499549/?ref_=fn_tt_tt_1" >Avatar</a> (2009) </td>
            </tr>
            <tr class="findResult even">
              <td class="primary_photo"><a href="/title/tt0417299/?ref_=fn_tt_tt_2" ><img src="http://ia.media-imdb.com/images/M/MV5BMTM3MTc3OTc0NF5BMl5BanBnXkFtZTcwOTQ0OTM1MQ@@._V1._CR34,0,295,440_SX32_CR0,0,32,44_AL_.jpg" /></a></td>
              <td class="result_text"><a href="/title/tt0417299/?ref_=fn_tt_tt_2" >Avatar: The Last Airbender</a> (2005) (TV Series) </td>
            </tr>

I can use PHP HTML DOM Parser but I'm learning regular expression. Here is my code

preg_match_all('!class="result_text"\s*>\s*<a href="/title/tt(?<imdbid>\d{7})/[^>]*>(?<title>.*?)</a>\s*(\([^\d{4}]\)\s*)?(\((?<year>\d{4})(.*?|)\)|)(?<type>[^<]*)!ims', $str, $matches);

Upvotes: 1

Views: 235

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627380

In general, it is not a bad idea to use regex to extract just URLs in some known tags with known format and in files that are not malformed, those you trust.

Thus, I do not like the idea to parse 2 adjacent tags with just 1 regular expression, but since you are learning:

<img\s[^>]*src="(?<imageURL>[^"]*)"\s*\/> # IMG tag
.*?                                       # Anything in-between IMG and A
<a\s[^>]*?href="\/title\/tt
 (?<imdbid>\d{7})                         # Got the imdbid
 \/[^>]*>(?<title>.*?)                    # Got title
 <\/a>                                    # End of A tag
 \s*\(
      (?<year>\d{4})                      # Year
 \)\s*(?:\(                               # Type is optional 
      (?<type>[^<]*)                      # Type
 \))?                                     # End of optional group

Mind that [^\d{4}] makes little sense, since you negate digits, {, 4 and {.

See demo

Code:

$re = "/<img\\s[^>]*src=\"(?<imageURL>[^\"]*)\"\\s*\\/> # IMG tag
.*?                                       # Anything in-between IMG and A
<a\\s[^>]*?href=\"\\/title\\/tt
  (?<imdbid>\\d{7})                        # Got the imdbid
  \\/[^>]*>(?<title>.*?)                   # Got title
  <\\/a>                                   # End of A tag
  \\s*\\(
       (?<year>\\d{4})                     # Year
   \\)\\s*(?:\\(                           # Type is optional 
      (?<type>[^<]*)                       # Type
     \\))?                                 # End of optional group/isx"; 
$str = "<tr class=\"findResult odd\">\n              <td class=\"primary_photo\"><a href=\"/title/tt0499549/?ref_=fn_tt_tt_1\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX32_CR0,0,32,44_AL_.jpg\" /></a></td>\n              <td class=\"result_text\"><a href=\"/title/tt0499549/?ref_=fn_tt_tt_1\" >Avatar</a> (2009) </td>\n            </tr>\n            <tr class=\"findResult even\">\n              <td class=\"primary_photo\"><a href=\"/title/tt0417299/?ref_=fn_tt_tt_2\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BMTM3MTc3OTc0NF5BMl5BanBnXkFtZTcwOTQ0OTM1MQ@@._V1._CR34,0,295,440_SX32_CR0,0,32,44_AL_.jpg\" /></a></td>\n              <td class=\"result_text\"><a href=\"/title/tt0417299/?ref_=fn_tt_tt_2\" >Avatar: The Last Airbender</a> (2005) (TV Series) </td>\n            </tr>"; 

preg_match_all($re, $str, $matches);

Upvotes: 1

Addison
Addison

Reputation: 8417

Consider trying:

preg_match_all('!<img src="(?<imageURL>[^"]*)"\s*>[.\s]*?class="result_text"\s*>\s*<a href="/title/tt(?<imdbid>\d{7})/[^>]*>(?<title>.*?)</a>\s*(\([^\d{4}]\)\s*)?(\((?<year>\d{4})(.*?|)\)|)(?<type>[^<]*)!ims', $str, $matches);

Upvotes: 0

Related Questions