tags:

views:

392

answers:

5

Duplicate: PHP validation/regex for URL

My goal is create a PHP regex for website name. The regex is for a lead gathering form and should accept any legit kind of website name syntax that someone might enter. After an exhaustive search, I'm surprised that I can't find one out there.

Here are the regex matches that I'm looking for:

AND, it should also match:

  • any of the above with a trailing backslash, such as: somewebsite.com/
  • subdomains
+8  A: 

No RegEx necessary.

var_dump(filter_var('example.com', FILTER_VALIDATE_URL));
mandaleeka
You're missing a close paren!
Matt Kane
Thanks, just proving why copy/pasting code is bad. :)
mandaleeka
Note that this is only for PHP 5.2+
Ólafur Waage
That is very useful, but it doesn't match the following:somewebsite.com
Andy, test your code before posting. That results in FALSE. I voted you up though because regex are so overused it's insane.
The Pixel Developer
A: 
/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i

http://www.shauninman.com/archive/2006/05/08/validating_domain_names

Courtesy of google. It is VERY complex though, so someone else might have a simpler one.

EDIT: Try andy's answer first. If you can find an alternative to a regex, 9/10 the alternative is much better.

Macha
That is very useful, but it doesn't match the following: somewebsite.com
A: 
^(https?://)?(([0-9a-z_!'().&=$%-]: )?[0-9a-z_!'().&=$%-]@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!'()-]\.)([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!*'().;?:@&=$,%#-])/?)$
Phill Pafford
I can't get your code to work. Can you provide a simple usage example?
$pattern = /^(https?://)?(([0-9a-z_!'().
Phill Pafford
Sorry, but still not working for me. This is what I am trying. Any suggestion?$some_url = 'http://some-url.com';$pattern = "/^(https?://)?(([0-9a-z_!'().?:@ if(preg_match($pattern, $some_url)) { echo "valid"; } else { echo "invalid"; }
+1  A: 

You might need to tweak it:

<?php

$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';

$url1  = "http://www.somewebsite.com";
$url2  = "https://www.somewebsite.com";
$url3  = "https://somewebsite.com";
$url4  = "www.somewebsite.com";
$url5  = "somewebsite.com";

function valURL($pattern, $url) {

        $return = false;

        if(preg_match($pattern, $url)) {
                $return = true;
        }

        if($return == true) {
                echo "Match URL: <font color='green'>" . $url . "</font><br /><br />";
        } else {
                echo "Try Again: <font color='red'>URL: " . $url . "</font><br /><br />";
        }
}

valURL($pattern, $url1);
valURL($pattern, $url2);
valURL($pattern, $url3);
valURL($pattern, $url4);
valURL($pattern, $url5);

?>
Phill Pafford
Yes, perfect! It works. Thank you.
+2  A: 
The Pixel Developer