PHP RegEx for "Site Name"

Duplicate: PHP validation / regex for URL

My goal is to create a PHP regex for the website name. A regex is for a lead collection form, and must accept any legal kind of website name syntax that someone might enter. After an exhaustive search, I'm surprised I can't find it there.

Below are the regex matches I'm looking for:

And, it must also match:

  • any of the above with a trailing backslash, for example: someebsite.com/
  • subdomains
+1


a source to share


5 answers


You may need to customize 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);

?>

      

+3


a source


No RegEx required.



$subject = 'example.com';
$part = (stripos($subject, 'http://') === FALSE)  ? 'http://' : '' ;
var_dump(filter_var($part.$subject, FILTER_VALIDATE_URL));

      

+8


a source


I decided to check the answers here to prove that regular expressions are not the answer for such simple tasks. Andy Lickman's code is 30% to 60% faster than other answers. It did have a bug, but I fixed it with a line of code. You can view my results below.

Here is the code that ran the tests.

http://pastie.org/476900

alt text http://img254.imageshack.us/img254/7821/capturevzh.png

PS If anyone uses regex to validate the url, I might go crazy;)

+3


a source


/^([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's VERY complex though, so someone else might have a simpler one.

EDIT: Please try and answer first. If you can find an alternative to regex, the 9/10 alternative is much better.

-1


a source


^(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_!*'().;?:@&=$,%#-])/?)$

      

-1


a source







All Articles