note 47291 added to function.eregi

From: Date: Wed, 10 Nov 2004 11:15:57 +0000
Subject: note 47291 added to function.eregi
Groups: php.notes 
Request: Send a blank email to php-notes+get-80340@lists.php.net to get a copy of this message
I was looking for something to validate a URL, and as usual turned to 'php.net' as it provides an invaluable source of information. I couldn't find exactly what I wanted so came up with something of my own which I hope will be of use to others. <?php function validateURL($URL) { $domain = "([[:alpha:]][-[:alnum:]]*[[:alnum:]]) (\.[[:alpha:]][-[:alnum:]]*[[:alpha:]])+"; $dir = "(/[[:alpha:]][-[:alnum:]]*[[:alnum:]])*"; $page = "(/[[:alpha:]][-[:alnum:]]*\.[[:alpha:]]{3,5})?"; $getstring = "(\?([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+) (&([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+))*)?"; $pattern = "^".$domain.$dir.$page.$getstring."$"; return eregi($pattern, $URL); } ?> Be sure to enter each pattern on a single line. The function accepts a URL and validates the domain, directory path, page and any GET parameters. You can see I have split the patterns for validating each section for easier reading and maintaining. Domain Name: This can be made of of many parts each seperated by a '.' (dot). each part must start with an alpha char. followed by any alphanumeric chars., or '-'. With the exception of the first part each part must end in an alpha char.. e.g. www.google.co.uk and www9.g-88gle.co.uk are valid, but 9ww.google.3o.uk and www.google4.co.uk3 are not. Directory Path: Each directory must start with a '/' followed by an alpha char., then any alpha numeric char., or '-', and ending in an alphanumeric char. e.g. /path1/to-page is valid, but /path1-/2-page is not. Page: This is like the directory path but it must end with a '.' (dot) followed by 3-5 alpha chars. e.g. /phpinfo.php and /page.shtml are valid, but /phpinfo.php4 and /pageshtml are not. Get String: This follows the page and begins with a '?'. This is then followed by key/value pairs each seperated by an '&'. e.g. ?key1=value1&key2=value2 Each key, or value must start and end with an alpahnumeric char. I have allowed for a '%' to be specified for URL encoded chars., and also '_' (underscore) is allowed. Well thats it hope it is of some use. It is NOT perfect, for instance in the 'getstring' I would like to ensure that encoded characters (i.e. %20) consist of a % symbol followed by two characters with values '0-9A-F' but couldn't get anything I tried to work. I would welcome any suggestions or improvements, there are bound to be some. Note: The patterns are based on an earlier post by 'fusillo at NOSPAM dot netgang dot it' for matching domain names. Thanks to him. ---- Manual Page -- http://www.php.net/manual/en/function.eregi.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47291 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47291&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47291&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80340) next »