please help me, i want to grab only the domain from this sting http://www.somedomain.com/cool/somefile.php Code (markup): i want to grab the "http://www.somedomain.com" Code (markup): any regex would help?
$text = 'http://www.somedomain.com/cool/somefile.php'; if (preg_match('/(https?:\/\/[^\/]+)/i', $text, $match)) { echo $match[1]; } else { echo 'not found'; } //or alternatively: if ($parsed = parse_url($text)) { echo 'http://' . $parsed['host']; } else { echo 'Incorrect URL'; } PHP:
Actually that previous regex is prone to errors. Make sure you anchor the regex by including a ^ at the start '/^(https.../
Here's something more universal with perl; [root@node5 ~]# perl -le '$url = "http://site.me/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;' http://site.me [root@node5 ~]# perl -le '$url = "https://site.me/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;' https://site.me [root@node5 ~]# perl -le '$url = "https://site.com/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;' https://site.com [root@node5 ~]# perl -le '$url = "https://site.info/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;' https://site.info [root@node5 ~]# perl -le '$url = "https://site.ERROR/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;' [root@node5 ~]# Code (markup):