Jump to content

regex for URL validating


fael097

Recommended Posts

hey folks!

i got this regex:

preg_match("/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i", $url

 

and its cool for regular urls, but it wont accept a slash at the end, or even slash something, like http://www.example.com/index.php

 

any clues how to add support to that?

the regex i really wanted is like this:

 

(optional: http(s):// or ftp://) [here you must put something, could be letters, numbers, dots, dashes, slashes, etc] [obligatory dot]  [here you must put something again, but only accept letters, and only 2 or 3 digits for endings like com, co, net, it, tk]{i dont know if numbers are needed, i've only seen extensions with 2 or 3 characters, and no numbers, please correct me}[and optionally anything again, like whatever can come after the .com, .co .net, could be anything like extension.php?variable=value]

 

hope i make myself clear enough!

thanks in advance people

Link to comment
Share on other sites

little fix: (double posted and cant edit first post anymore)

 

(optional: http(s):// or ftp://) [here you must put something, could be letters, numbers, dots, dashes, slashes, etc] [obligatory dot]  [here you must put something again, but only accept letters, and only 2 or 3 digits for endings like com, co, net, it, tk]{i dont know if numbers are needed, i've only seen extensions with 2 or 3 characters, and no numbers, please correct me}[and optional, anything that can come after the .com, .co .net, like slashes, filenames and variables, for example extension.php?variable=value]

Link to comment
Share on other sites

wow, i only color coded to make it easier to understand, thanks a lot for making fun of it.

 

@mchl: thanks! i totally forgot about .info

 

regex needed: (in order, this time without colors, and dont make fun of my english, its not my 1st language but im sure you can understand it)

1-optional http(s):// or ftp://

2-anything that a person can put on a website address, that probably excludes some special characters

3-dot

4-letters only, 2 - 4 digits, for the .us / .com / .info part

5-anything again, for the extension of the address, like filenames, variables etc, except special characters again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.