您可以使用正則表達式,
例子:live
$domainRegex = '/^[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$/';
$urlRegex = '/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/';
$a = array('http://domain.com/page/ex.html',
'http://domain.com/',
'domain.com'
);
foreach ($a as $v) {
if (preg_match($urlRegex, $v)) {
echo $v.': url<br/>';
} else if (preg_match($domainRegex, $v)) {
echo $v.': domain<br/>';
}
}
輸出應該是:
http://domain.com/page/ex.html: url
http://domain.com/: url
domain.com: domain
傳遞字符串到[parse_url(http://www.php.net/parse_url),檢查哪些部分是這樣。 –