亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php將url地址轉(zhuǎn)化為完整的a標(biāo)簽鏈接代碼(php為url地址添加a標(biāo)簽)

 更新時(shí)間:2014年01月17日 11:38:36   作者:  
這篇文章主要介紹了php為url地址添加a標(biāo)簽的示例,大家參考使用吧

需要提取的內(nèi)容如下:

復(fù)制代碼 代碼如下:

<a >http://baidu.com</a>這是第一個(gè)A標(biāo)簽,
<a 。
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個(gè)IMG標(biāo)簽

類似微博中的自動(dòng)提取URL為超鏈接地址。即內(nèi)容提取出來(lái)添加A標(biāo)簽,轉(zhuǎn)換成真正的超鏈接。網(wǎng)上搜索了很久,沒(méi)有找到一個(gè)切實(shí)可行的解決方案。大都只是簡(jiǎn)單的提取URL(A標(biāo)簽和IMG標(biāo)簽內(nèi)的地址也被提取替換了),并不能滿足以上需求。正則表達(dá)式中也沒(méi)發(fā)現(xiàn)能夠?qū)崿F(xiàn)提取時(shí)過(guò)濾掉A標(biāo)簽的方法。于是轉(zhuǎn)換了一下思路,“曲線救國(guó)”。即,先將所有的A標(biāo)簽和IMG標(biāo)簽正則替換為某一個(gè)統(tǒng)一的標(biāo)記,然后再提取URL地址替換為超鏈接,最后再將統(tǒng)一的標(biāo)記還原替換為以前的A標(biāo)簽和IMG標(biāo)簽便解決了。

復(fù)制代碼 代碼如下:

function linkAdd($content){
 //提取替換出所有A標(biāo)簽(統(tǒng)一標(biāo)記<{link}>)
 preg_match_all('/<a.*?href=".*?".*?>.*?</a>/i',$content,$linkList);
 $linkList=$linkList[0];
 $str=preg_replace('/<a.*?href=".*?".*?>.*?</a>/i','<{link}>',$content);

 //提取替換出所有的IMG標(biāo)簽(統(tǒng)一標(biāo)記<{img}>)
 preg_match_all('/<img[^>]+>/im',$content,$imgList);
 $imgList=$imgList[0];
 $str=preg_replace('/<img[^>]+>/im','<{img}>',$str);

 //提取替換標(biāo)準(zhǔn)的URL地址
 $str=preg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)','<a href="\0" target="_blank">\0</a>',$str);

 //還原A統(tǒng)一標(biāo)記為原來(lái)的A標(biāo)簽
 $arrLen=count($linkList);
 for($i=0;$i<$arrLen;$i++){
  $str=preg_replace('/<{link}>/',$linkList[$i],$str,1);
 }

 //還原IMG統(tǒng)一標(biāo)記為原來(lái)的IMG標(biāo)簽
 $arrLen2=count($imgList);
 for($i=0;$i<$arrLen2;$i++){
  $str=preg_replace('/<{img}>/',$imgList[$i],$str,1);
 }

 return $str;
}

$content='
<a >http://baidu.com</a>這是第一個(gè)A標(biāo)簽,
<a >成長(zhǎng)腳印-專注于互聯(lián)網(wǎng)發(fā)展</a>這是第二個(gè)A標(biāo)簽。
http://chabaoo.cn這是第一個(gè)需要被提取的URL地址,
http://blog.baidu.com這是第二個(gè)需要被提取的URL地址。
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個(gè)IMG標(biāo)簽';
echo linkAdd($content);


返回的內(nèi)容為:

復(fù)制代碼 代碼如下:

<a >http://baidu.com</a>這是第一個(gè)A標(biāo)簽, <a >成長(zhǎng)腳印-專注于互聯(lián)網(wǎng)發(fā)展</a>這是第二個(gè)A標(biāo)簽。 <a href="http://chabaoo.cn" target="_blank">http://chabaoo.cn</a>這是第一個(gè)需要被提取的URL地址, <a target="_blank">http://blog.baidu.com</a>這是第二個(gè)需要被提取的URL地址。
<img border="0" alt="" src="http://baidu.com/css/sitelogo_zh-cn.gif">,這是一個(gè)IMG標(biāo)簽

即為我們想要的內(nèi)容。

例2,

復(fù)制代碼 代碼如下:

/**
 * PHP 版本 在 Silva 代碼的基礎(chǔ)上修改的
 * 將URL地址轉(zhuǎn)化為完整的A標(biāo)簽鏈接代碼
 */

function replace_URLtolink($text) {
    // grab anything that looks like a URL...
    $urls = array();

    // build the patterns
    $scheme = '(https?://|ftps?://)?';
    $www = '([w]+.)';
    $ip = '(d{1,3}.d{1,3}.d{1,3}.d{1,3})';
    $name = '([w0-9]+)';
    $tld = '(w{2,4})';
    $port = '(:[0-9]+)?';
    $the_rest = '(/?([w#!:.?+=&%@!-/]+))?';
    $pattern = $scheme.'('.$ip.$port.'|'.$www.$name.$tld.$port.')'.$the_rest;
    $pattern = '/'.$pattern.'/is';

    // Get the URLs
    $c = preg_match_all($pattern, $text, $m);

    if ($c) {
        $urls = $m[0];
    }

    // Replace all the URLs
    if (! empty($urls)) {
        foreach ($urls as $url) {
            $pos = strpos('http://', $url);

            if (($pos && $pos != 0) || !$pos) {
                $fullurl = 'http://'.$url;
            } else {
                $fullurl = $url;
            }

            $link = ''.$url.'';

            $text = str_replace($url, $link, $text);
        }
    }

    return $text;
}

相關(guān)文章

最新評(píng)論