2011-05-18 36 views
1
http://www.trafficestimate.com/,http://getclicky.com/,http://technotarget.com/find-out-who-is-visiting-your-site-website-traffic-tools/,http://pmetrics.performancing.com/ 

以上是用于修整的示例网站。我只想从上面提取域名,例如:trafficestimate.com,getclicky.com,technotarget.com,performancing.com如何修剪此段

我该怎么用PHP做这件事?我正在谈论更多像这样的网址,而不仅仅是上面的网址。

回答

7

当然,让我们看看如何做到这一点。首先,我们需要将这些URL分解为单独的组件。我们可以通过使用explode命令做到这一点:

$urls = "http://www.trafficestimate.com/,http://getclicky.com/,http://technotarget.com/find-out-who-is-visiting-your-site-website-traffic-tools/,http://pmetrics.performancing.com/"; 

$url_array = explode(",", $urls); 

这样做是拿你的网址,并把它们放到一个数组将它们在逗号分隔。让我们来看看示例结果是什么样的:

Array 
(
    [0] => http://www.trafficestimate.com/ 
    [1] => http://getclicky.com/ 
    [2] => http://technotarget.com/find-out-who-is-visiting-your-site-website-traffic-tools/ 
    [3] => http://pmetrics.performancing.com/ 
) 

漂亮的呃?现在,下一步是循环所有的结果,这可以通过一个简单的foreach循环完成。但在我们做之前,我们需要在某个地方存储结果域。我们声明空数组:

$domains = array(); 

现在我们可以遍历结果:

$domains = array(); 
foreach($url_array as $url) { 
    // actions here 
} 

那么,我们需要什么。对于每个结果呢?我们需要域名。 PHP实际上有一个很好的功能来解析称为parse_url的网址。替代方案是使用更复杂的措施,所以这很好地工作!这里是我们更新后的代码:

$domains = array(); 
foreach($url_array as $url) { 
    $parsed_url = parse_url($url); 
} 

那么现在,让我们看看parse_url给我们:

Array 
(
    [scheme] => http 
    [host] => pmetrics.performancing.com 
    [path] =>/
) 

注意主机?这是我们试图抓住的域名。因此,我们将它添加到我们的域阵列:

$domains = array(); 
foreach($url_array as $url) { 
    $parsed_url = parse_url($url); 
    $domains[] = $parsed_url['host']; 
} 

现在让我们看看结果是什么:

Array 
(
    [0] => www.trafficestimate.com 
    [1] => getclicky.com 
    [2] => technotarget.com 
    [3] => pmetrics.performancing.com 
) 

这就是它! $domain现在拥有所有的域名。如果我们想打印他们用逗号像上面分开,我们可以使用implode命令这样做:

echo implode(',', $domains); 

这给了我们:

www.trafficestimate.com,getclicky.com,technotarget.com,pmetrics.performancing.com 

而这一切有太多啦!以下是完整的代码清单,供大家参考:

$urls = "http://www.trafficestimate.com/,http://getclicky.com/,http://technotarget.com/find-out-who-is-visiting-your-site-website-traffic-tools/,http://pmetrics.performancing.com/"; 

$url_array = explode(",", $urls); 

$domains = array(); 
foreach($url_array as $url) { 
    $parsed_url = parse_url($url); 
    $domains[] = $parsed_url['host']; 
} 

echo implode(',', $domains); 
+0

感谢您的详细解释 – john 2011-05-18 02:44:00

2

像这样:

$input = explode(',', $input); 

,然后为每个值:

$input[$k] = preg_replace('/^https?://(?:www\.)?/i', '', $input[$k]); 
1
<?php 
// get host name from URL 
preg_match("/^(http:\/\/)?([^\/]+)/i", 
    "http://www.example.com/index.html", $matches); 
$host = $matches[2]; 

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

/* Output is example.com */ 

?> 
+0

我怎么在数组做到这一点? – john 2011-05-18 02:31:43

0

或者您可以使用此功能只得到域。

function GetDomain($url) 
{ 
$nowww = ereg_replace('www\.','',$url); 
$domain = parse_url($nowww); 
if(!empty($domain["host"])) 
    { 
    return $domain["host"]; 
    } else 
    { 
    return $domain["path"]; 
    } 

} 
0
$urls = 'http://www.trafficestimate.com/,http://getclicky.com/,http://technotarget.com/find-out-who-is-visiting-your-site-website-traffic-tools/,http://pmetrics.performancing.com/'; 
$hosts = array_map(function ($url) { return parse_url($url, PHP_URL_HOST); }, explode(',', $urls)); 

var_dump($hosts); 

注意它返回pmetrics.performancing.com例如,这是正确的方式做到这一点,虽然。没有规定说只有顶级域名和第一个子域名是“域名”,完整的主机名是域名。

0
<?php 
$input = explode(',', $input); 
$urls = array(); 
foreach($input as $item){ 
    $url = parse_url($item); 
    $urls[] = $item[host]; 
} 
?>