-1

Possible Duplicate:
PHP validation/regex for URL

Hi,

in my form I have a url field. The user my input all kind of web url's. I use the following regex to validate the url:

/^(https?):\/\/(?:[A-Z0-9-]+.)+[A-Z]{2,6}([\/?].+)?$/i

But this regex handles some url's as invalid, e.g. the following

http://partners.webmasterplan.com/click.asp?ref=537448&site=5643&type=b479&bnb=479

Any ideas how to improve the regex?

Community
  • 1
  • 1
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • 2
    Have a look at http://stackoverflow.com/questions/206059/php-validation-regex-for-url – John Carter Nov 28 '10 at 14:49
  • -1 for being lazy. The first answer StackOverflow suggested when entering "Regex for url validation" as question title answers your question. Please do not litter StackOverflow with duplicates. – Gordon Nov 28 '10 at 15:59

3 Answers3

2

why dont use filter_var

filter_var('example.com', FILTER_VALIDATE_URL))

do not use regular expressions where is's not necessary

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
1

This problem has been solved many times before. See here, for example.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
1

As for your regex, the first period needs to be escaped like this

/^(https?):\/\/(?:[A-Z0-9-]+\.)+[A-Z]{2,6}([\/?].+)?$/i
Barry Simpson
  • 544
  • 3
  • 4