Jump to content

validate::isUrl($url) is it correct ?


Bru

Recommended Posts

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?

:mellow:

Bru

Link to comment
Share on other sites

  • 11 months later...

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);

  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...