par
sylvercis » 15 juil. 2012, 10:40
Finalement j'ai trouvé une regex sur le net qui a l'air de bien fonctionner:
Code : Tout sélectionner
function isValidURL($url) {
// SCHEME
$urlregex = "#^(https?|ftp)\:\/\/";
// USER AND PASS (optional)
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
// HOSTNAME OR IP
$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum
//$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}"; // http://x.xx(x) = minimum
//use only one of the above
// PORT (optional)
$urlregex .= "(\:[0-9]{2,5})?";
// PATH (optional)
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
// GET Query (optional)
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?";
// ANCHOR (optional)
$urlregex .= "(\#[a-z_.-][a-z0-9+\$_.-]*)?\$#";
// check
if (preg_match($urlregex, $url)) {return true;} else {return false;}
}
// valid urls
$url = "
https://user:[email protected]:808 ... 23#pagetop";
$url = "
http://[email protected]/#pagetop";
$url = "
https://somewhere.com/index.html";
$url = "
ftp://user:****@somewhere.com:21/";
$url = "
http://somewhere.com/index.html/"; //this is valid!!
il y a encore quelques rares url qui ne passent pas avec mais pour la plupart c'est ok, je pourrai toujours l'améliorer plus tard suivant les retours que j'aurai a l'utilisation.
merci pour ton aide !
Finalement j'ai trouvé une regex sur le net qui a l'air de bien fonctionner:
[code]
function isValidURL($url) {
// SCHEME
$urlregex = "#^(https?|ftp)\:\/\/";
// USER AND PASS (optional)
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
// HOSTNAME OR IP
$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // http://x.x = minimum
//$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}"; // http://x.xx(x) = minimum
//use only one of the above
// PORT (optional)
$urlregex .= "(\:[0-9]{2,5})?";
// PATH (optional)
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
// GET Query (optional)
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?";
// ANCHOR (optional)
$urlregex .= "(\#[a-z_.-][a-z0-9+\$_.-]*)?\$#";
// check
if (preg_match($urlregex, $url)) {return true;} else {return false;}
}
[/code]
// valid urls
$url = "https://user:
[email protected]:8080/login.php?do=login&style=%23#pagetop";
$url = "http://
[email protected]/#pagetop";
$url = "https://somewhere.com/index.html";
$url = "ftp://user:****@somewhere.com:21/";
$url = "http://somewhere.com/index.html/"; //this is valid!!
il y a encore quelques rares url qui ne passent pas avec mais pour la plupart c'est ok, je pourrai toujours l'améliorer plus tard suivant les retours que j'aurai a l'utilisation.
merci pour ton aide !