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

Php 比较URL数组中的主机名并获取唯一值,php,arrays,unique,distinct,Php,Arrays,Unique,Distinct,我需要比较url并从数组中删除重复项,但我只想比较url中的主机。我需要跳过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.face

我需要比较url并从数组中删除重复项,但我只想比较url中的主机。我需要跳过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,'/')));
            });
但它返回空数组。

尝试以下方法:

<?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;
}


修剪并仅保留主机名

您需要通过URL循环,使用PHP的
URL\u Parse()
函数解析URL,并使用array\u unique从数组中删除重复项,因此我们正在检查主机和路径

我为你写了一节课:

<?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_一次。这个类也是关于PHP类的,做它是为了好玩


祝你好运

可能会添加regex标签。看一看,但是你在哪里可以给我展示工作示例或任何想法?我需要在不使用www的情况下进行比较,并需要选择的数组。我还有一个问题,如果我想将主机名与路径进行比较,并且只想比较google.com/test怎么办?基本上我们用于提取url,此函数还返回路径。只需修改parseURLS fn一点点即可检查路径值。如果我想比较登录页,我的问题是更新。你只需将此连接起来。$parse[“path”];我已经更新了这个类。它看起来很棒,但是如果路径后面有查询怎么办。有时我想,如果没有WWWI,URL有时只需要GooLo.COM/Test/GooGeL.COM/EnthutkWo.W.W.W.W.W.W.T.S.Wr.To来比较我的URLs,你可以简单地从结果中删除[Stand ],然后使用StrRePosiy()来删除WWW PAR或者可选地使用PrimgMatHeMe(),对不起,我正在工作,但我想你还没有找到一个对你有帮助的答案1.我把我的问题改了。例如,我需要检查数组中是否有google.com/test,如果我有重复的,那么删除你的代码效果很好,但我需要在主机名之后与所有登录页进行比较。我用一个新的正则表达式更新了我的答案。如果它为你工作,请接受它。它仍然是不一样的,我需要删除所有之前的域名。在主机名“google,facebook”等所有内容被删除之前,我尝试了一些类似于^ https?:/(www\\)?|的想法。所有http://都将从域名和上删除并启动。检查我放在这里的输出,并提供您希望看到的输出。我在数组示例中看到了您的更新,也更新了我的数组。从你的描述来看,这正是你所需要的。保留域和后,并比较所有其余的。
<?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
        )

)
<?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);

?>