php提取IMDb搜索

时间:2015-04-16 06:48:39

标签: php regex

我需要帮助才能使用下面的字符串提取照片网址

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

我可以使用PHP HTML DOM Parser,但我正在学习正则表达式。这是我的代码

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);

2 个答案:

答案 0 :(得分:1)

一般情况下,使用正则表达式来提取一些已知格式的已知标记中的URL以及没有格式错误的文件(您信任的文件)并不是一个坏主意。

因此,我不喜欢用1个正则表达式解析2个相邻标签的想法,但是因为你正在学习:

<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

请注意[^\d{4}]没有意义,因为您否定了数字,{4{

请参阅demo

代码:

$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);

答案 1 :(得分:0)

考虑尝试:

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);