Jump to content

reg expression for URL


TheOne17

Recommended Posts

I have been using this function to validate an URL from field

 

function checkURL($url)

{

return eregi("^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$", $url);

}

 

The only problem is that is won't accept URL's with a hyphen in

e.g.

www.northlincs.gov.uk/20-21

 

Does anyone know how to modify this to work with hyphens or have a good reg exp for URL's that will work with hyphens

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/41257-reg-expression-for-url/
Share on other sites

<?php
$url="http://www.google.co.uk";
if(eregi("^(http://|https://|ftp://|
http://www.)+[a-z0-9\_\-]{1,25}\.+
([a-z0-9\_\-]{1,3}|.+[a-z0-9\_\-]{1,3}\.
+[a-z0-9\_\-]{1,3})$", $url)
){
echo" your url is: $url";
}else{
echo"sorry wrong url: $url format";
}
?>

 

This will do what ever but only for the index of the domain not what your trying to do if

you want to your need to alter the code and add another or | ok.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.