...

View Full Version : Regular expression help



four0four
02-10-2010, 01:19 AM
I'm trying to create a regular expression that validates a URL.

For example, a valid URL in this case would match:

http(s)://www.yahoo.com/file.php?name=some-thing+some_thing+some~thing&page=1#anchor

So basically the forward slash after the TLD can contain: a-zA-Z0-9#?&=+-:~/._

This is what I have so far:



$urlpattern = '/^(http(s?):\/\/)((\w+\.)+)\w{2,}(\/?)$/i';


Thanks!

kbluhm
02-10-2010, 01:31 AM
Why not just parse the URL with parse_url() and examine the values returned? Let PHP do the heavy lifting for you. ;)
http://www.php.net/parse_url

Or... filter_var():
http://www.php.net/filter_var


if ( FALSE === filter_var( $url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED ) )
{
// URL is invalid
}

Untested, but should work.

Lamped
02-10-2010, 01:57 PM
define('NET_URL_REGEX',
'@^
(?# Protocol)
([a-z]{1,6}://)
(?# Login)
(?:([^\:]*)(?:\:([^\@]*))?\@)?
(?# Domain)
([a-z0-9][a-z\.\-]*[a-z])
(?# Port)
(\:[0-9]{1,5})?
(?# Path)
(/[a-zA-Z0-9\$\-\_\.\+\!\'\(\|\)\,\;\/\@\&\=\%]* )?
(?# Query)
(\?[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\|\)\,\;\/\?\:\@\&\=\%]*)?
$@Sx'
);

function net_split_url($url) {
/* Near perfect uri matching and splitting */
if (preg_match(NET_URL_REGEX,$url,$matches)) {
$details = array(
'protocol' => $matches[1],
'username' => $matches[2],
'password' => $matches[3],
'domain' => $matches[4],
'path' => $matches[5],
'query' => $matches[6]
);
if ($details['query']) {
$query = explode('&',$details['query']);
$details['query'] = array();
foreach($query as $item) {
list($name,$value) = explode('=',$item,2);
$details['query'][urldecode($name)] = urldecode($value);
}
}
return $details;
} else {
return False;
}


I wrote this ages ago. If you preg_match(NET_URL_REGEX, $url) that should be a very reliable check for URL validity.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum