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
https://forums.phpfreaks.com/topic/206180-regex-for-url-validating/
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]

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.