regex for URL validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

regex for URL validation

Post by fael097 »

hey folks!
i got this regex:

Code: Select all

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:

(http(s):// or ftp://) [here could be letters, numbers, dots, dashes, slashes, etc] dot [here would accept letters 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 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex for URL validation

Post by AbraCadaver »

Why a regex?

Code: Select all

filter_var('http://www.example.com/index.php', FILTER_VALIDATE_URL);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: regex for URL validation

Post by fael097 »

AbraCadaver wrote:Why a regex?

Code: Select all

filter_var('http://www.example.com/index.php', FILTER_VALIDATE_URL);
that is even worse than my regex, it will only require a http:// in front of it, it will validate "http://adsfkjchawuifhw"
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex for URL validation

Post by AbraCadaver »

fael097 wrote:
AbraCadaver wrote:Why a regex?

Code: Select all

filter_var('http://www.example.com/index.php', FILTER_VALIDATE_URL);
that is even worse than my regex, it will only require a http:// in front of it, it will validate "http://adsfkjchawuifhw"
Actually it is "better" for a URL which is what you asked for. A URL must start with a scheme (in this case http://) followed by a hostname (in this case adsfkjchawuifhw). So maybe you don't want to match URLs, but specific types of URLs or partial URLs?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply