我想知道这是否是一种正确的方式来检查一个字符串是否仅包含URL:
if (stripos($string, 'http') == 0 && !preg_match('/\s/',$string)) {
do_something();
}
stripos()函数检查字符串是否以“http”开头
preg_match()函数检查字符串是否包含空格
如果不是这样,我假设该字符串仅为URL - 但是这个假设有效吗?有更好的方法来实现这个吗?
我想知道这是否是一种正确的方式来检查一个字符串是否仅包含URL:
if (stripos($string, 'http') == 0 && !preg_match('/\s/',$string)) {
do_something();
}
stripos()函数检查字符串是否以“http”开头
preg_match()函数检查字符串是否包含空格
如果不是这样,我假设该字符串仅为URL - 但是这个假设有效吗?有更好的方法来实现这个吗?
filter_var()
函数。if (filter_var($string, FILTER_VALIDATE_URL)) {
// you're good
}
å
的URL,但是该URL正在工作。(使用PHP 7.0) - TheStoryCoderhttp://www.php.net/manual/en/function.filter-var.php
if(filter_var('http://example.com', FILTER_VALIDATE_URL)) {
echo 'this is URL';
}
Check with the filter (be sure to check the manual on which filter suits your situation)
Check to see if there are DNS records
$string = idn_to_ascii($URL);
if(filter_var($string, FILTER_VALIDATE_URL) && checkdnsrr($string, "A")){
// you have a valid URL
}