开发者

Validate if input is link

I want to check if the entered is a link o开发者_高级运维r not.

$s = $_POST['link'];

check if $s is a link.


You can use filter_var()

filter_var('example.com', FILTER_VALIDATE_URL);


There are a few ways, but the most robust would be to use a regular expression to detect a URL.

Something like this would work well:

$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/'; 

$isLink = preg_match($pattern, $url); // Returns true if a link


function isValidURL($url) {
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

if(!isValidURL($fldbanner_url)) {
    $errMsg .= "* Please enter valid URL including http://<br>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜