這是個獲取文章內(nèi)容中所有鏈接的php正則表達式
$str ="";
?代碼如下 | 復(fù)制代碼 |
$reg = "[^"']*))|(? |
//下面這個實例是獲取內(nèi)容中域名正則表達式
?代碼如下 | 復(fù)制代碼 |
function get_domain($url){ ? $pattern = "/[w-]+.(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/"; ? preg_match($pattern, $url, $matches); ? if(count($matches) > 0) { ?? return $matches[0]; ? }else{ ?? $rs = parse_url($url); ?? $main_url = $rs["host"]; ?? if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) { ??? return $main_url; ?? }else{ ??? $arr = explode(".",$main_url); ??? $count=count($arr); ??? $endArr = array("com","net","org","3322");//com.cn? net.cn 等情況 ??? if (in_array($arr[$count-2],$endArr)){ ???? $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1]; ??? }else{ ???? $domain =? $arr[$count-2].".".$arr[$count-1]; ??? } ??? return $domain; ?? } ? } ?} |
?
?//實例
?
?$str ="jfkdlajfdafdjak;www.111cn.net";
?echo get_domain($str)
?
?//得出值為111cn.net
?
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號