Created
April 18, 2011 13:52
-
-
Save edmondscommerce/925381 to your computer and use it in GitHub Desktop.
snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Scratch</h1> | |
<pre> | |
<?php | |
date_default_timezone_set('UTC'); | |
$official_pattern='/^[\w\.]+\.[a-zA-Z]{2,4}$/'; | |
$official_pattern_2='/^[\w\.]+\.[a-zA-Z]{2,4}(?:|\/.{3,20})$/'; | |
$pattern = "%"; | |
//match a www or other subdomain followed by a dot if its there, | |
//not required though, from very start of subject | |
$pattern.="\A([\w]+\.){0,1}"; | |
//match 1 or more word characters - this is the domain name | |
$pattern.="([\w]+)"; | |
//dot followed by between 2 and 3 word characters, repeated 1 or 2 times | |
$pattern .="(\.[\w]{2,3}){1,2}"; | |
//very end of subject | |
$pattern .= "\Z"; | |
$pattern.="%"; | |
$subjects[] = 'www.bbc.co.uk'; | |
$subjects[] = 'news.bbc.co.uk'; | |
$subjects[] = 'im not a url'; | |
$subjects[] = 'www.google.com'; | |
$subjects[] = 'test.co.uk'; | |
$subjects[] = '.co.uk'; | |
foreach ($subjects as $subject) { | |
echo '<h1>' . $subject . '</h1>'; | |
$res = preg_match($pattern, $subject, $matches); | |
echo '<h2>' . (($res) ? 'Matched' : 'Not Matched') . '</h2>'; | |
var_dump($matches); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment