php教程 匹配url 正則運算式代碼
這是個獲取文章內容中所有連結的php正則運算式
$str ="";
$reg = "<a[sS]*? (href)s*=s*(?(? =["'])((["'])(?<href>[^"']*)2)| (?<src>[^s>]+)) [sS]*?>";//下面這個實例是獲取內容中功能變數名稱正則運算式
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 = par se_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.jzread.com";
echo get_domain($str)
//得出值為jzread.com