首頁 > 後端開發 > php教程 > 正则表述提取标签属性值

正则表述提取标签属性值

WBOY
發布: 2016-06-13 13:06:39
原創
995 人瀏覽過

正则表达提取标签属性值
如何用正则表达是提title的值“The Amazing Spider-Man (2012)”呢?
我自己是用两次preg_split取的,但感觉很低效,有没有朋友给点思路呢?

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<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"    style="max-width:90%"  style="max-width:90%" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>
  </td>

登入後複製


------解决方案--------------------
PHP code

<?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"    style="max-width:90%"  style="max-width:90%" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>
    <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)">
  ';
$patten = '/title="(.*)"/Uis';
if (preg_match ( $patten, $str, $array )) {
print_r($array);
}
echo "<br>";
$patten1 = '/</a><a href=".*" title="(.*)">/Uis';
if (preg_match_all ( $patten1, $str, $array1 )) {
print_r($array1[1]);
}
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
<dl class="code">PHP code<pre class="brush:php;toolbar:false">

$string='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)1"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg"    style="max-width:90%"  style="max-width:90%" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)3"></a><a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)2"></a>
</td>'; 
    if (preg_match_all ( '/(?array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(38) "title="The Amazing Spider-Man (2012)1""
    [1]=>
    string(38) "title="The Amazing Spider-Man (2012)3""
    [2]=>
    string(38) "title="The Amazing Spider-Man (2012)2""
  }
  [1]=>
  array(3) {
    [0]=>
    string(30) "The Amazing Spider-Man (2012)1"
    [1]=>
    string(30) "The Amazing Spider-Man (2012)3"
    [2]=>
    string(30) "The Amazing Spider-Man (2012)2"
  }
}

    */ <div class="clear">
                 
              
              
        
            </div>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板