如何将包含URL的数组拆分为组?

时间:2015-03-03 17:59:21

标签: php arrays

我有一个数组,其中包含许多不同网站的网址。有没有办法自动拆分它们

例如,转过来:

http://url-one.com/page1.html
http://url-one.com/page3.html
http://url-one.com/page5.html
http://url-two.com/file/test.rar
http://url-two.com/file/test22.rar
http://url-two.com/file/test33.rar
http://url-three.net/pages/download/file/1
http://url-three.net/pages/download/file/2
http://url-three.net/pages/download/file/2

进入这个:

url-one.com links:
http://url-one.com/page1.html
http://url-one.com/page3.html
http://url-one.com/page5.html

url-two.com links:
http://url-two.com/file/test.rar
http://url-two.com/file/test22.rar
http://url-two.com/file/test33.rar

url-three.net links:
http://url-three.net/pages/download/file/1
http://url-three.net/pages/download/file/2
http://url-three.net/pages/download/file/2

我并不总是知道主机是什么,因此我无法手动输入主机以匹配它。

2 个答案:

答案 0 :(得分:4)

$urlList[] = 'http://url-one.com/page1.html';
$urlList[] = 'http://url-two.com/file/test.rar';
$urlList[] = 'http://url-two.com/file/test.rar';
$urlList[] = 'http://url-two.com/file/test22.rar';
$urlList[] = 'http://url-three.net/pages/download/file/1';

$groupedList = array();

foreach($urlList as $url){
    $parse = parse_url($url);
    $groupedList[$parse['host']][] = $url;
}

foreach($groupedList as $group){
    echo "<div>\n";
        foreach($group as $url){
            echo $url."<br>\n";
        }
    echo "</div>\n";
}

/* Output:
<div>
http://url-one.com/page1.html<br>
</div>
<div>
http://url-two.com/file/test.rar<br>
http://url-two.com/file/test.rar<br>
http://url-two.com/file/test22.rar<br>
</div>
<div>
http://url-three.net/pages/download/file/1<br>
</div>
*/

编辑:每条评论请求,一种过滤域名的方法。由于我将主机存储为密钥,因此我使用了array_diff_keyarray_flip。 PHP 5.6添加了一种将密钥传递给array_filter的方法,这样可以使事情变得更容易。

$filterDomainList[] = 'url-one.com';
$filterDomainList[] = 'url-three.net';

$filtered = array_diff_key( $groupedList, array_flip( $filterDomainList ) );
var_dump($filtered);

/*array(1) {
  ["url-two.com"]=>
  array(3) {
    [0]=>
    string(32) "http://url-two.com/file/test.rar"
    [1]=>
    string(32) "http://url-two.com/file/test.rar"
    [2]=>
    string(34) "http://url-two.com/file/test22.rar"
  }
}
*/

答案 1 :(得分:0)

您可以在检查数组中的每个项目是否以某个字符串(例如“http://url-three.net/”)开头并在此基础上循环遍历数组时将此项添加到某个数组

foreach($urls as $url){
    if ( strpos($url, "http://url-one.net") > 0)
        ; // add to 1st array
    else if ( strpos($url, "http://url-two.net") > 0)
        ; // add to 2nd array
    else if ( strpos($url, "http://url-three.net") > 0)
        ; // add to 3rd array
}