You are here: Re: How to extract domain from string with regex? « PHP Programming Language « IT news, forums, messages
Re: How to extract domain from string with regex?

Posted by deko on 11/21/60 11:57

Thanks for the tip on parse_url.

But I still have to find the URL (which could be anywhere) in the string.

> Without the spaces? Then, why do you add the spaces?

Here's a psuedocode example without the spaces:

if (eregi("http://", $mystring))
{
$mystring = explode("http://", $mystring);
$mystring = array_reverse($mystring);
$domain = $mystring[0];
$domain = explode(".", $domain);
if ($domain[0] == "www")
{
$extracted = $domain[1].".".$domain[2];
}
else
{
$extracted = "$domain[0].".".$domain[1];
}
}

Would it be better to use preg_match here?

preg_match('@^(?:http://)?([^/]+)@i',
"http://www.php.net/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";

But would this work if the URL is buried in a string?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация