php - Regex to match domain failing on input -
i created regex match domain name out of input:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i'; $url = 'http://wwe.com'; if (preg_match($pattern, $url, $matches) === 1) { echo $matches[0]; }
it works fine input:
http://google.com // output:google.com
but not able achieve these inputs: (if user enters www
http://www.google.com // output:google.com http://www.www.google.com // output:google.com
what missing?
any on appreciated
what this?
<?php $urls = [ 'http://google.com', 'http://www.google.com', 'http://www.www.google.com', ]; foreach($urls $url) { $url = parse_url($url, php_url_host); $url = preg_replace('/^(www\.)+/', '', $url); echo $url . "\n"; }
output:
google.com google.com www.google.com
Comments
Post a Comment