<?php
$str='<td class="image">
    <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)"></a>
  </td>';
$patten = '/title="(.*)"/Uis';
if (preg_match ( $patten, $str, $array )) {
print_r($array);
}
为什么结果是Array ( [0] => title="The Amazing Spider-Man (2012)" [1] => The Amazing Spider-Man (2012) ) 
也就是Array[0]和Array[1]的值是怎么来的?

解决方案 »

  1.   

    preg_match($pattern,$string,$matcher)其中$pattern对应的就是/title="(.*)"/Uis,$string 是'<td class="image">
        <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)"></a>
      </td>,$matcher是匹配到的结果。如果提供了 $matcher,则其会被搜索的结果所填充。$matcher[0] 将包含与整个模式匹配的文本,$matcher[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。