溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用PHP獲取一個(gè)頁面上的鏈接信息

發(fā)布時(shí)間:2020-07-23 19:20:17 來源:網(wǎng)絡(luò) 閱讀:1018 作者:gutaotao1989 欄目:web開發(fā)

開發(fā)中我們可能會(huì)獲取某個(gè)頁面或是一段內(nèi)容中的鏈接信息,下面我分享一個(gè)我寫的函數(shù)給大家,希望能幫到大家。


函數(shù)功能:

1、獲取一段內(nèi)容中鏈接信息;

2、獲取一個(gè)URL中鏈接信息;

3、剔除錨鏈等無效的鏈接

4、獲取當(dāng)前域下的鏈接信息

5、獲取他域下的鏈接信息

6、保留鏈接的文本信息

代碼:

/**
 * +----------------------------------------------------------
 * 功能:獲取一個(gè)網(wǎng)頁或一段內(nèi)容里面的鏈接信息
 * +----------------------------------------------------------
 * @param string $html    要獲取鏈接的內(nèi)容或網(wǎng)址
 * @param string $isExclude  是否過濾無效的鏈接,如"","#","javascript:;","javascript:void(0);" 。默認(rèn)過濾
 * @param string $isKeepLinkText 是否保留鏈接的文字。默認(rèn)保留,保留與不保留鏈接數(shù)可能不同
 * @param string $linkType    取得鏈接的類型,all所有的鏈接,inner 本域下的鏈接, out 外域的鏈接信息。默認(rèn) 是取得所有鏈接
 * +----------------------------------------------------------
 * @return array
 * +----------------------------------------------------------
 */
function getLinks($html,$isExclude=true,$isKeepLinkText=true,$linkType='all'){
    if(empty($html)) return false;
    set_time_limit(0);//防止超時(shí)
    $removes=array('','#','javascript:;','javascript:void(0);','javascript:void(0)');//排除錨鏈之類的
    $html = substr(strtolower($html),0,4)=="http"?file_get_contents($html):$html;//要處理的內(nèi)容
    //提取鏈接信息
    $pattern = '/<a(?:.*?)href="(((?:http(?:s?):\/\/)?([^\"\/]+))?(?:[^\"]*))"(?:[^>]*?)>([^<]*?)<\/a>/i';
    preg_match_all($pattern, $html, $_links);
    if($isKeepLinkText){
        foreach ($_links[1] as $key => $href) {
            $links[$_links[4][$key]]=$href;
        }
    }else{
        $links=$_links[4];
    }
    unset($_links);
                    
    foreach ($links as $text => $href) {
        //移除無效的鏈接
        if($isExclude&&in_array($href, $removes)) {           
            unset($links[$text]);
        }
        if($linkType!='all'){
            $host=parse_url($href); 
            $host=isset($host['host'])?$host['host']:'';
            if($linkType=='inner'){//本域鏈接
                if(substr($href,0,1)!="/"&&strtolower($host)!=strtolower($_SERVER['SERVER_NAME'])) {
                    unset($links[$text]);
                }
            }elseif($linkType=='out'){//他域鏈接
                if(substr($href,0,1)=="/"||strtolower($host)==strtolower($_SERVER['SERVER_NAME'])) {
                    unset($links[$text]);
                }
            }
        }
    }
    return $links;
}

使用方法:

$links=getLinks("http://www.sina.com.cn");
               
//或
 $links=getLinks("http://www.sina.com.cn",1,0,"out");
               
 //或
 $links=getLinks("這里是一段要提取鏈接信息的內(nèi)容");

特別說明:

1、上面的函數(shù)用到了file_get_contents ,獲取內(nèi)容可能會(huì)失敗,你可以自行改成curl;

2、提取鏈接用了正則,效率可能低。


當(dāng)然你也看一看使用下面的函數(shù),當(dāng)要提取的內(nèi)容網(wǎng)址的時(shí)候不使用正則來提取鏈接信息:

代碼:

/**
 * +----------------------------------------------------------
 * 功能:獲取一個(gè)網(wǎng)頁或一段內(nèi)容里面的鏈接信息
 * +----------------------------------------------------------
 * @param string $html    要獲取鏈接的內(nèi)容或網(wǎng)址
 * @param string $isExclude  是否過濾無效的鏈接,如"","#","javascript:;","javascript:void(0);" 。默認(rèn)過濾
 * @param string $isKeepLinkText 是否保留鏈接的文字。默認(rèn)保留,保留與不保留鏈接數(shù)可能不同
 * @param string $linkType    取得鏈接的類型,all所有的鏈接,inner 本域下的鏈接, out 外域的鏈接信息。默認(rèn) 是取得所有鏈接
 * +----------------------------------------------------------
 * @return array
 * +----------------------------------------------------------
 */
function getLinks($html,$isExclude=true,$isKeepLinkText=true,$linkType='all'){
    if(empty($html)) return false;
    set_time_limit(0);
    $removes=array('','#','javascript:;','javascript:void(0);','javascript:void(0)');//排除錨鏈之類的
    $isLink=substr(strtolower($html),0,4)=="http"?1:0;//是否是鏈接
    $html = $isLink?file_get_contents($html):$html;
    if($isLink){
        $dom = new DOMDocument();
        @$dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        unset($dom);
        $hrefs = $xpath->evaluate("/html/body//a");//獲取a節(jié)點(diǎn)
        $length=$hrefs->length;//獲取鏈接數(shù)
        $links=array();//網(wǎng)頁上的鏈接
        for ($i = 0; $i < $length; $i++) {
            $href = trim($hrefs->item($i)->getAttribute('href'));
            $text=trim($hrefs->item($i)->textContent);
            $links[$text]=$href;
        }
    }else{
        $pattern = '/<a(?:.*?)href="(((?:http(?:s?):\/\/)?([^\"\/]+))?(?:[^\"]*))"(?:[^>]*?)>([^<]*?)<\/a>/i';
        preg_match_all($pattern, $html, $_links);
        if($isKeepLinkText){
            foreach ($_links[2] as $key => $href) {
                $links[$_links[4][$key]]=$href;
            }
        }else{
            $links=$_links[4];
        }
        unset($_links);
    }
    foreach ($links as $text => $href) {
        //移除無效的鏈接
        if($isExclude&&in_array($href, $removes)) {
            unset($links[$text]);
        }
        if($linkType!='all'){
            $host=parse_url($href); 
            $host=isset($host['host'])?$host['host']:'';
            if($linkType=='inner'){//本域鏈接
                if(substr($href,0,1)!="/"&&strtolower($host)!=strtolower($_SERVER['SERVER_NAME'])) {
                    unset($links[$text]);
                }
            }elseif($linkType=='out'){//他域鏈接
                if(substr($href,0,1)=="/"||strtolower($host)==strtolower($_SERVER['SERVER_NAME'])) {
                    unset($links[$text]);
                }
            }
        }
    }
    return $links;
}

使用方法與上面一致

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI