Bru Posted December 18, 2011 Share Posted December 18, 2011 I am programming some module. In this module I need test if input URL is valid. So, I find class ValidateCore in Validate.php. There is function isUrl * Check url valdity (disallowed empty string) * * @param string $url Url to validate * @return boolean Validity is ok or not */ public static function isUrl($url) { return preg_match('/^[~:#,%&_=\(\)\.\? \+\-@\/a-zA-Z0-9]+$/', $url); } here regular expression will match every string which contain one or more characters ~|:|#|,|%|&|_|=|(|)|.|?| |+|-|@|/|a-z|A-Z|0-9| e.g. validate::isUrl("this is not url") return 1 validate::isUrl("http://www.prestashop.com/forums/") return 1 validate::isUrl("::/#?") return 1 validate::isUrl(":*:/#?") return 0 validate::isUrl(" ") return 1 So I am confused, if this function really check url valdity. Is there other function for url validation or I should write my own? Bru Link to comment Share on other sites More sharing options...
Visiedo Posted December 15, 2012 Share Posted December 15, 2012 (edited) Definitely it is not correct! I would rather use a custom validation here. E.g. preg_match("^(http|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$",$url) Edited December 15, 2012 by Visiedo (see edit history) Link to comment Share on other sites More sharing options...
Visiedo Posted December 15, 2012 Share Posted December 15, 2012 Some improvements and documentation: $validURL = preg_match("/^ # Start at the beginning of the text (?:ftp|https?):\/\/ # Look for ftp, http, or https (?: # Userinfo (optional) (?:[\w\.\-\+%!$&'\(\)*\+,;=]+* [\w\.\-\+%!$&'\(\)*\+,;=]+@ )? (?:[a-z0-9\-\.%]+) # The domain (?::[0-9]+)? # Server port number (optional) (?:[\/|\?][\w#!:\.\?\+=&%@!$'~*,;\/\(\)\[\]\-]*)? # The path (optional) $/xi", $url); 1 Link to comment Share on other sites More sharing options...
Recommended Posts