Page 1 of 1
Regex that matches domain/subdomain
Posted: Sun Jun 06, 2004 1:24 pm
by Rahil
Does anyone have a regex that will match a domain, and another regex that will match a subdomain? I've tried searching google, and many PHP websites/forums but found nothing. Any help is appreciated.

Posted: Mon Jun 07, 2004 12:59 am
by scorphus
Posted: Mon Jun 07, 2004 11:49 pm
by scorphus
Could also try this one:
Code: Select all
<?php
$url = 'http://www2.smarty.php.net/index.php';
$regexp = '/^(((?:ht|f)tp):\/\/)?(www?[0-9]*?\.)?(([a-zA-Z0-9][[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]]?)\.)?([a-zA-Z0-9][[[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]]?\.]*[a-zA-Z]{2,6})(\/.*)?$/';
preg_match($regexp, $url, $matches);
print_r($matches);
?>
output:
Code: Select all
Array
(
ї0] => http://www2.smarty.php.net/index.php
ї1] => http://
ї2] => http
ї3] => www2.
ї4] => smarty.
ї5] => smarty
ї6] => php.net
ї7] => /index.php
)
-- Scorphus.
Posted: Sun Jun 13, 2004 9:37 pm
by Rahil
Thanks guys.