我要把一个页面中所有的文字链接取出来..
比如一段HTML里有下面的链接.<a href="http://www.sohu.com" target="_blank">搜狐</a><a href="http://www.sina.com" target="_blank">新浪</a>........
........
........则要显示成为Array
(
    [0] => Array
        (
            [0] => http://www.sohu.com
            [1] => 搜狐
        )    [1] => Array
        (
            [0] => http://www.sina.com
            [1] => 新浪
        )
    ...
    ...
    ...
)

解决方案 »

  1.   

    <?php
    $contents = '<a href="http://blog.yodao.com/search?q=%E8%82%A1%E6%8C%874000%E7%82%B9&t=a&keyfrom=163index">股指4000点</a>';
    if (1) {
    $handle = fopen("http://www.163.com/", "rb");
    $contents = "";
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
    $contents = iconv("GB2312", "UTF-8", $contents);
    }
    if (preg_match_all("/<a(?:[^<>]*)href=(?:[\"\']?)([^<>\"\']*)(?:[\"\']?)\s?(?:[^<>]*)>([^<>]*)<\/a>/im", $contents, $res, PREG_SET_ORDER)) {
        print_r($res);
    }
    ?>试一下这个吧。测试可以抓出99%以上,不过写在js里面的那种
    var link = '<a href=\"http://cha.so.163.com/so.php?key=001001006&q=%D6%D0%B9%FA%B7%AD%D2%EB%CD%F8\">中国翻译网</a>'; 就另当别论了。
    结果形如:    [58] => Array
            (
                [0] => <a href="http://co.163.com/">土木在线</a>
                [1] => http://co.163.com/
                [2] => 土木在线
            )    [59] => Array
            (
                [0] => <a href="http://vip.163.com/">VIP邮箱</a>
                [1] => http://vip.163.com/
                [2] => VIP邮箱
            )    [60] => Array
            (
                [0] => <a href="http://biz.163.com/">商业</a>
                [1] => http://biz.163.com/
                [2] => 商业
            )
      

  2.   

    <?php
    header("Content-Type: text/html; Charset=utf-8");$str='<a href="http://www.sohu.com" target="_blank">搜狐</a><a href="http://www.sina.com" target="_blank">新浪</a>
    ';
    preg_match_all("/<a href=\"([^\"]*)[^>]*>(.*)<\/a>/",$str,$array);
    echo "<pre>";
    print_r($array);?>
    $array的顺序自己调整