<h3 class="r"><a href="http://www.JAVA.com/" target=_blank class=l onmousedown="IID()">Java <b>...</b>....</a>。
<h3 class="r"><a href="http://www.C#.com/" target=_blank class=l onmousedown="IID()">C#</a>........
<h3 class="r"><a href="http://www.php.com/" target=_blank class=l onmousedown="IID()">php</a>........
<h3 class="r"><a href="http://www.asp.com/" target=_blank class=l onmousedown="IID()">asp</a>........例如上面一段代码,一个html页面里面有许多“<h3 class="r">”,我想把所有“<h3 class="r">”后面的第一个链接(<a></a>)中的网址和内容分别取出来。
比如第一个,<h3 class="r"><a href="http://www.JAVA.com/" target=_blank class=l onmousedown="IID()">Jaca <b>...</b>....</a>。
我想得到的是http://www.JAVA.com和Java ,依此类推全有的“<h3 class="r">”后面的第一个链接(<a></a>)中的网址和内容都分别取出来。帮帮忙吧

解决方案 »

  1.   

    如果<h3>内有不定多个<a>的话,用html dom或xml dom+xpath 效果更好如果你确定字符串从"<h3"开始到第一个"</a>"都有固定格式的话才适用正则"/<h3.*<a href=\"([^\"]+)\"/isU"
      

  2.   


    $str=<<<eof
    <h3 class="r"><a href="http://www.JAVA.com/" target=_blank class=l onmousedown="IID()">Java <b>...</b>....</a>。
    <h3 class="r"><a href="http://www.C#.com/" target=_blank class=l onmousedown="IID()">C#</a>........
    <h3 class="r"><a href="http://www.php.com/" target=_blank class=l onmousedown="IID()">php</a>........
    <h3 class="r"><a href="http://www.asp.com/" target=_blank class=l onmousedown="IID()">asp</a>........
    eof;
    echo '<pre />';
    if(preg_match_all('/<h3[^>]*><a\s+href="(.*?)"/i',$str,$arr)){
      print_r($arr[1]);
    }
    输出:
    Array
    (
        [0] => http://www.JAVA.com/
        [1] => http://www.C#.com/
        [2] => http://www.php.com/
        [3] => http://www.asp.com/
    )
      

  3.   

    后面的Java,C#文字怎么一起得到呢?
      

  4.   

    7楼的稍微改下
    <?php
    $str=<<<eof
    <h3 class="r"><a href="http://www.JAVA.com/" target=_blank class=l onmousedown="IID()">Java <b>...</b>....</a>。
    <h3 class="r"><a href="http://www.C#.com/" target=_blank class=l onmousedown="IID()">C#</a>........
    <h3 class="r"><a href="http://www.php.com/" target=_blank class=l onmousedown="IID()">php</a>........
    <h3 class="r"><a href="http://www.asp.com/" target=_blank class=l onmousedown="IID()">asp</a>........
    eof;
    echo '<pre />';
    if(preg_match_all('/<h3[^>]*><a\s+href="(http:\/\/www.(.*?).com)\/"/i',$str,$arr)){
      print_r($arr[1]);
      print_r($arr[2]);
    }?>
      

  5.   

    .com这里是不是也需要改一下?.net什么会有问题吗?
      

  6.   

    if(preg_match_all('/<h3[^>]*><a\s+href="(http:\/\/www.([^.]*).(.*))\/"/i',$str,$arr)){
      print_r($arr[1]);
      print_r($arr[2]);
    }