yes (www\.|) is like (www\.)?
Edit: Added below:
Here is a useful little code snippet to test regexs. You can add as many regexs as you want:
Code:
$pat=Array(
"com"=> "/^(http:\/\/)?(www\.)?([^.]*?)\.com/",
"s com"=> "/^(http(s)?:\/\/)?(www\.)?([^.]*?)\.com/",
"sub"=> "/^(http(s)?:\/\/)?(www\.)?([^.]*?\.)?([^.]*?)\.com/",
"multiple domain"=> "/^(http(s)?:\/\/)?(www\.)?([^.]*\.)?([^.]*?)(\..{2}\..{2}|\..{3})/"
);
$padding=" ";
$string="https://bob.site.co.uk";
echo "String: <strong>".$string."</strong><br><br>";
foreach($pat as $k=>$v) {
$pad=$padding;
echo 'Pattern <strong>'.$k.'</strong> <em>' . $v . '</em><br>'.$pad;
if ( preg_match($v, $string, $matches) ) {
echo 'Match: ';
echo cust_print($matches, $pad, 2);
} else {
echo 'No Match';
}
echo "<br><br>";
}
function cust_print($a, $p="", $n=1) {
if(!is_array($a)) {
return $a;
}
$s="Array(";
foreach($a as $k=>$v) {
$s.="<br>".str_repeat($p, $n).'['.$k.'] => '.cust_print($v, $p, $n+1);
}
$s.="<br>".str_repeat($p, $n-1).")";
return $s;
}
Bookmarks