简体   繁体   中英

PHP url preg_match

I have a problem with this preg_match

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

For these links should display - true

/test.html
/testowa-strona_9067.html
/567890.html?get=test
/costam.html?get=2&f[]=k&f[]=k2 

And for those false

/.html
/ąęśćzmn-ż.html
/testmhtml
/%67%68%89(i&.html?get=34 

But it always displays true

You should try this :

if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
    die('Not a valid URL');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM