PHP-php中如何使用正则表达式匹配URL中的域名

2025-06-27 16:06:00
推荐回答(1个)
回答1:

// 从 URL 中取得主机名 
preg_match("/^(http:\/\/)?([^\/]+)/i", "IP/index.html", $matches); 
$host = $matches[2]; 
// 从主机名中取得后面两段 
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches); 
echo "domain name is: {$matches[0]}\n"; 
?>