<h3 class="r"><a href="http://www.163.com/" target=_blank class=l onmousedown="IID()">网易 <b>...</b>....</a>。
<h3 class="r"><a href="http://www.sina.com/" target=_blank class=l onmousedown="IID()">新浪</a>........
<h3 class="r"><a href="http://www.qq.com/" target=_blank class=l onmousedown="IID()">腾讯</a>........例如上面一段代码,一个html页面里面有许多“<h3 class="r">”,我想把所有“<h3 class="r">”后面的第一个链接(<a></a>)中的内容取出来。
比如第一个,<h3 class="r"><a href="http://www.163.com/" target=_blank class=l onmousedown="IID()">网易 <b>...</b>....</a>。
我想得到的是网易 ,依此类推全有的“<h3 class="r">”后面的第一个链接(<a></a>)中的内容都取出来。帮帮忙吧.

解决方案 »

  1.   

    这种用法用dom + xpath多好啊
    $dom = new DOMDocument('1.0', 'GBK');
    $dom->loadHTML($url);$ret = array();$xpath = new DOMXPath($dom);$query = '//html/body..../h3@[class="r"]/a';$entries = $xpath->query($query);何必非要正则呢,不过你既然要了就给你吧
    <h3 class="r"><a href="http://www.sina.com/" target=_blank class=l onmousedown="IID()">新浪</a><h3\s*class="r"\s*>\s*<a[^>]+>(.*?)</a>
      

  2.   

    preg_match_all('/<h3[\r\n\s]+class\s*=\s*"r">[\r\n\s]*(<a[^>]+>(.*)<\/a>)/isU', $str, $matches);print_r($matches[1]);print_r($matches[2]);