比如有 以下几个url
<a href=' http://www.baidu.com'>AA</a>
<a href=' http://www.google.com'>BB</a>
<a href=' http://www.hao123.com'>CC</a>
<a href=' http://www.126.com'>AA</a>这里有四个a标签,标签的显示名有 AA,BB,CC,AA
我想要提取标签显示名为 AA 的,怎样提取啊?在PHP里正则表达式如何写?谢谢正确结果是:http://www.baidu.com和http://www.126.com

解决方案 »

  1.   

    $str = <<<STR
    <a href=' http://www.baidu.com'>AA </a> 
    <a href=' http://www.google.com'>BB </a> 
    <a href=' http://www.hao123.com'>CC </a> 
    <a href=' http://www.126.com'>AA </a> 
    STR;
    preg_match_all("/<a[^<>]*href='([^']+)'[^<>]*>AA[\s]+<\/a>/im", $str, $matches);
    var_dump($matches[1]);
      

  2.   

    $subject = "<a href=' http://www.baidu.com'>AA </a>
    <a href=' http://www.google.com'>BB </a>
    <a href=' http://www.hao123.com'>CC </a>
    <a href=' http://www.126.com'>AA </a> ";$pattern = '@<a(.*?)>(.[^>]*?)</a>@im';preg_match_all($pattern,$subject,$matches);
    print_r($matches[0]);
      

  3.   


    $subject = "<a href=' http://www.baidu.com'>AA</a>
    <a href=' http://www.google.com'>BB</a>
    <a href=' http://www.hao123.com'>CC</a>
    <a href=' http://www.126.com'>AA</a> ";$pattern = '@<a(.*?)>(.[^>]*?)</a>@im';preg_match_all($pattern,$subject,$matches);
    foreach($matches[2] as $_key => $_value){
        if($_value == 'AA') echo $matches[1][$_key].'</br>';
    }
      

  4.   

    $subject = "<a href=' http://www.baidu.com'>AA </a>
    <a href=' http://www.google.com'>BB </a>
    <a href=' http://www.hao123.com'>CC </a>
    <a href=' http://www.126.com'>AA </a> ";$pattern = '@<a(.*?)>AA[\s]+</a>@im';preg_match_all($pattern,$subject,$matches);echo '<pre>';
    print_r($matches[1]);
    echo '</pre>';
      

  5.   

    $str = <<<STR
    <a href=' http://www.baidu.com'>AA </a> 
    <a href=' http://www.google.com'>BB </a> 
    <a href=' http://www.hao123.com'>CC </a> 
    <a href=' http://www.126.com'>AA </a> 
    STR;
    preg_match_all("/<a[^<>]*href=['\"]([^'\"]+)['\"][^<>]*>AA[\s]+<\/a>/im", $str, $matches);
    var_dump($matches[1]);
      

  6.   

    靠, 终于出来了$subject = "<a class=\"sdfsdf\" href=' http://www.baidu.com' sdf=sdf >AA </a>
    <a href=' http://www.google.com'>BB </a>
    <a href=' http://www.hao123.com'>CC </a>
    <a href=' http://www.126.com' sdf=sdfsd >AA </a> ";$pattern = "@<a[^<>]*href=['\"]?([^'\"]+)['\"]?[^<>]*>AA[\s]+</a>@im";preg_match_all($pattern,$subject,$matches);echo '<pre>';
    print_r($matches[1]);
    echo '</pre>';