比较URL数组中的主机名并获取唯一值

时间:2017-09-27 08:54:47

标签: php arrays unique distinct

我需要比较网址并从数组中删除重复项,但我想只比较来自网址的主机。当我比较时,我需要跳过http和https以及www和其他像最后一个斜杠。 所以,当我有阵列时:

    $urls = array(
'http://www.google.com/test', 
'https://www.google.com/test',
'https://www.google.com/example', 
'https://www.facebook.com/example',
'http://www.facebook.com/example');

结果仅为

http://www.google.com/test
http://www.google.com/example
http://www.facebook.com/example

我试着比较:

$urls = array_udiff($urls, $urls, function ($a, $b) {
                 return strcmp(preg_replace('|^https?://(www\\.)?|', '', rtrim($a,'/')), preg_replace('|^https?://(www\\.)?|', '', rtrim($b,'/')));
            });

但它让我回到空阵。

3 个答案:

答案 0 :(得分:1)

<?php
   $urls = array(
    'http://www.google.com/test',
    'https://www.google.com/test',
    'https://www.google.com/example',
    'https://www.facebook.com/example',
    'http://www.facebook.com/example');


$MyArray = [];
for($i=0;$i<count($urls);$i++)  {

preg_match_all('/www.(.*)/', $urls[$i], $matches);

    if (!in_array($matches[1], $MyArray))
        $MyArray[] = $matches[1];
}

echo "<pre>";
print_r($MyArray);
echo "</pre>";

输出

Array
(
    [0] => Array
        (
            [0] => google.com/test
        )

    [1] => Array
        (
            [0] => google.com/example
        )

    [2] => Array
        (
            [0] => facebook.com/example
        )

)

修剪并仅保留主机名

答案 1 :(得分:0)

尝试这种方法:

<?php
function parseURLs(array $urls){
    $rs = [];
    foreach($urls as $url){
        $segments = parse_url($url);
        if(!in_array($segments['host'], $rs))
            $rs[] = $segments['host'];
    }
    return $rs;
}

然后:

<?php
$urls = array(
    'http://www.google.com',
    'https://www.google.com',
    'https://www.google.com/',
    'https://www.facebook.com',
    'http://www.facebook.com'
);
$uniqueURLs = parseURLs($urls);
print_r($uniqueURLs);

/* result :
Array
(
    [0] => www.google.com
    [1] => www.facebook.com
)
*/

答案 2 :(得分:0)

您需要使用PHP的url_parse()函数循环访问URL,解析URL并使用array_unique从数组中删除重复项,因此我们检查主机和路径.. < / p>

我为你写了一堂课:

<?php
/** Get Unique Values from array Values **/
Class Parser {
    //Url Parser Function
    public function arrayValuesUrlParser($urls) {
        //Create Container
        $parsed = [];
        //Loop Through the Urls
        foreach($urls as $url) {
            $parse = parse_url($url);
            $parsed[] = $parse["host"].$parse["path"];
            //Delete Duplicates
            $result = array_unique($parsed);
        }
        //Dump result
        print_r($result);
    }

}

?>

使用班级

<?php
//Inlcude tghe Parser
include_once "Parser.php";

    $urls = array(
    'http://www.google.com/test', 
    'https://www.google.com/test',
    'https://www.google.com/example', 
    'https://www.facebook.com/example',
    'http://www.facebook.com/example');
    //Instantiate
    $parse = new Parser();
    $parse->arrayValuesUrlParser($urls);

?>

如果您不需要分隔文件,则可以在一个文件中执行此操作,但如果您使用的是一个php文件,则必须删除include_once。这个类也在PHP Classes上,为了好玩而做到了!

最好的运气!