Author Topic: Does anyone have a function for validating URL entry.  (Read 433 times)

0 Members and 1 Guest are viewing this topic.

Offline bagTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Does anyone have a function for validating URL entry.
« on: December 09, 2009, 08:04:11 AM »
Users have the capability of entering URLs and I have used the following

$pattern = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
if ( preg_match($pattern, $url) == FALSE ) {

It got a bit out of hand and I'm lost.  This one fails to validate valid urls and url validation must be something that has been done to death.

Offline JAY6390

  • Devotee
  • Posts: 804
  • Gender: Male
  • Not suitable for adults
    • View Profile
    • Jay Gilford
Re: Does anyone have a function for validating URL entry.
« Reply #1 on: December 09, 2009, 08:12:38 AM »
why not just use filter_var() with the FILTER_VALIDATE_URL parameter?

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Does anyone have a function for validating URL entry.
« Reply #2 on: December 09, 2009, 08:30:43 AM »
I've not really looked at your pattern in any detail, but it should throw an error. PCRE patterns (ie preg_ functions) require delimiters. A search on google for validating URL should turn up millions of possible patterns, it mainly depends on how accurate you wish to be and which URLs you wish to support ie protocols (http://, ftp:// etc), domains (.co.uk, .info etc), parameters (?id=10&name=bob) and so on.

If you are using PHP 5.2 or newer then it would probably make more sense to use filter_var() as suggested by JAY6390.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: Does anyone have a function for validating URL entry.
« Reply #3 on: December 09, 2009, 09:15:39 AM »
… it mainly depends on how accurate you wish to be and which URLs you wish to support ie protocols (http://, ftp:// etc), domains (.co.uk, .info etc), parameters (?id=10&name=bob) and so on.

… it would probably make more sense to use filter_var() as suggested by JAY6390.

With regards to the filter extension, the quoted portion of your first paragraph is just as relevant as with the regex approach. The FILTER_VALIDATE_URL filter will accept a wide variety of URLs and it's likely the application will want to accept a subset only. That said, for a first-step the filter works as advertised.

Just to clarify, FILTER_VALIDATE_URL will happily accept URLs like the following (and many more):
  • abcdefghjiklmnopqrstuvwxz://.
  • foo://!
  • news:
  • file:_=~-_$.$_-~=_
  • .://.
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline JAY6390

  • Devotee
  • Posts: 804
  • Gender: Male
  • Not suitable for adults
    • View Profile
    • Jay Gilford
Re: Does anyone have a function for validating URL entry.
« Reply #4 on: December 09, 2009, 09:19:26 AM »
Yup. It may also be worth looking into parse_url() to check portions of the url are as you wish (such as the scheme)

Offline bagTopic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Does anyone have a function for validating URL entry.
« Reply #5 on: December 09, 2009, 09:24:08 AM »
Thanks for the quick responses.

We are on 5.1 here but I'll look at the parse_url function.  That looks like something I could deal with instead of those patterns.   

Offline JAY6390

  • Devotee
  • Posts: 804
  • Gender: Male
  • Not suitable for adults
    • View Profile
    • Jay Gilford
Re: Does anyone have a function for validating URL entry.
« Reply #6 on: December 09, 2009, 09:29:25 AM »
It certainly does make easy work of urls, and means you don't need those messy regexes for them