【问题标题】:Compare host name from array of URLs and get unique values比较 URL 数组中的主机名并获取唯一值
【发布时间】:2017-09-27 08:54:47
【问题描述】:

我需要比较 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,'/')));
            });

但它返回给我一个空数组。

【问题讨论】:

  • 也许添加正则表达式标签。
  • 看看this
  • 但是你在哪里可以给我展示工作示例或任何想法?
  • 我需要比较没有 www 并且需要选择数组

标签: php arrays unique distinct


【解决方案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
        )

)

修剪并仅保留主机名

【讨论】:

  • 我更新了我的问题。我需要将所有内容与主机名和主机名之后的所有内容进行比较,例如www/google.com/test 我需要检查数组中是否有 google.com/test,如果我有重复项,则删除您的代码效果很好,但我需要与所有登陆页面进行比较主机名
  • 我用新的正则表达式更新了我的答案。如果它适合你,请接受它。
  • 它仍然不一样,我需要在域名之前删除所有内容。我尝试了一些想法,例如 |^https?://(www\\.)?|
  • 在主机名之前,“google , facebook”等所有内容都被删除。所有 http:// 都被删除并从域名开始。检查我放在这里的输出并提供你想看到的输出。
  • 我在数组示例中看到了您的更新,也更新了我的数组。根据您的描述,这正是您所需要的。保留域和之后并比较所有其余部分。
【解决方案2】:

试试这个方法:

<?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
)
*/

【讨论】:

  • 我还有一个问题,如果我想将 hostanme 与路径进行比较,例如 google.com/test 并且只想比较 google.com/test 怎么办?
  • 基本上我们使用parse_url来提取url,这个函数也返回路径。只需稍微修改 parseURLS fn 以检查路径值。 here
【解决方案3】:

您需要遍历 URL,使用 PHP 的 url_parse() 函数解析 URL 并使用 array_unique 从数组中删除重复项,因此我们正在检查主机和路径..

我为你写了一个类:

<?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 上,是为了好玩!

祝你好运!

【讨论】:

  • 如果我想比较登陆页面,我的问题是更新
  • 你只需连接这个 .$parse["path"];我已经更新了课程..
  • 看起来很棒,但如果路径后会有查询怎么办。第二个认为它需要这个 www。我有时有没有 www 的网址
  • 我只需要获取 google.com/test 或 google.com/everythinkwhatwillbewroteafterthisslash 来比较我的网址
  • 您可以简单地从结果中删除 ["scheme"],然后使用 str_replace() 删除 www par 或使用 preg_match(),对不起,我在工作,但我没有t 找到答案会帮助你在 evining ..
猜你喜欢
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 2013-10-11
  • 2016-02-15
  • 2021-04-13
  • 1970-01-01
相关资源
最近更新 更多