Forum Moderators: coopster

Message Too Old, No Replies

Regular expression for url check

regular expression url check

         

bramcorleone

8:10 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



I have found a function on the internet for checking url's, but this url check function doesn't contain a regular expression for this kind of url's:

[sub.domain.com...]
(mainly the part after dir (I guess))

This is the function:
function control_url($in) {

$start_url = "(http(s)?\:\/\/)?"; // start URL
$dots = "([\w_-]{2,}\.)+"; // one or more parts containing a '.' at the end
$last_part = "([\w_-]{2,})"; // last part doesn't contain a dot
$user = "((\/)(\~)[\w_-]+)?((\/)[\w_-]+)*"; // maybe subdirectories - possibly with user ~
$end = "((\/)¦(\/)[\w_-]+\.[\w]{2,})?"; // maybe a slash at the end or slash+file+extension
$qstring1 = "((\?[\w_-]+\=([^\#]+)){0,1}"; // querystring - first argument (?a=b)
$qstring2 = "(\&[\w_-]+\=([^\#]+))*)?"; // querystring - following arguments (&c=d)
$bkmrk = "(#[\w_-]+)?"; // bookmark

$exp = "/^".$start_url.$dots.$last_part.$user.$end.$qstring1.$qstring2.$bkmrk."$/i";
return preg_match($exp, $in);
}

Does anyone know the regular expression to get aboving url (which is indeed correct) through?

PHP_Chimp

10:35 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just need to modify -
$user = "((\/)(\~)[\w_-]+)?((\/)[\w_-]+)*"; // maybe subdirectories - possibly with user ~

To -

$user = "((\/)(\~)[\w_=-]+)?((\/)[\w_=-]+)*";

To add the = to the character class.