有没有办法将所有 while-loop 值存储到数组中?

Is there a way to store all the while-loop values into the array?

我正在制作一个非常全面的应用程序,我已经为此工作了几个月。对于我的下一步,我正在编写一些代码,将从我的文本文件中删除所有重复的域。

我之前所做的是使用 (php) array_unique(); 函数,该函数会从我的 txt 文件中删除完全相同的副本。但是我需要删除所有相同的域。

旧情况(这将删除 url 1 或 2 因为它们完全相同):

  1. google.nl
  2. google.nl
  3. google.nl/hello

期望的情况(将删除三个中的两个 url,因为域相同):

  1. google.nl/hello
  2. google.nl/yellow
  3. google.nl

所以我编写了一些代码,将我的 txt 文件中的每个 url 显示到屏幕上(没什么特别的)。我通过使用 while 循环来做到这一点:

$file = fopen("file.txt","r");
while(! feof($file))
{
    echo fgets($file). "<br />";
}

所以我使用本教程来帮助自己:how to get domain name from URL。这是我使用的代码。

function parse_url_all($url){
    $url = substr($url,0,4)=='http'? $url: 'http://'.$url;
    $d = parse_url($url);
    $tmp = explode('.',$d['host']);
    $n = count($tmp);
    if ($n>=2){
        if ($n==4 || ($n==3 && strlen($tmp[($n-2)])<=3)){
            $d['domain'] = $tmp[($n-3)].".".$tmp[($n-2)].".".$tmp[($n-1)];
            $d['domainX'] = $tmp[($n-3)];
        } else {
            $d['domain'] = $tmp[($n-2)].".".$tmp[($n-1)];
            $d['domainX'] = $tmp[($n-2)];
        }
    }
    return $d;
}

$urls = array('website1','website2');
echo "<div style='overflow-x:auto;'>";
echo "<table style='text-align:left;'>";
echo "<tr><th>URL</th><th>Host</th><th>Domain</th><th>Domain X</th></tr>";
foreach ($urls as $url) {
    $info = parse_url_all($url);
    echo "<tr><td>" . $url . "</td><td>" . $info['host'] . "</td><td>" . $info['domain'] . "</td><td>" . $info['domainX'] . "</td></tr>";
}
echo "</table></div><br>";

如何将我的 while 循环 (txtfile) 的输出从这一行获取到数组中: $urls = array('output from textfile'); 可能会很简单,但我就是想不通。

这是您可以用来查找唯一网址并将其保存为 csv 文件的代码段:

<?php
function parse_url_all($url)
{
    $url = substr($url, 0, 4) == 'http' ? $url : 'http://' . $url;
    $d = parse_url($url);
    $tmp = explode('.', $d['host']);
    $n = count($tmp);
    if ($n >= 2)
    {
        if ($n == 4 || ($n == 3 && strlen($tmp[($n - 2) ]) <= 3))
        {
            $d['domain'] = $tmp[($n - 3) ] . "." . $tmp[($n - 2) ] . "." . $tmp[($n - 1) ];
            $d['domainX'] = $tmp[($n - 3) ];
        }
        else
        {
            $d['domain'] = $tmp[($n - 2) ] . "." . $tmp[($n - 1) ];
            $d['domainX'] = $tmp[($n - 2) ];
        }
    }
    return $d;
}


// read file and unique urls in array.
$urls = array();
$input_file = "urls.txt";
$handle = fopen($input_file, "r");
if ($handle)
{
    while (($line = fgets($handle)) !== false)
    {
        $parsed_url = parse_url_all(trim($line));
        // 'domain' is unique (i assume)
        $urls[$parsed_url["domain"]] = $parsed_url;
    }

    fclose($handle);
}
else
{
    // error opening the file.

}


// write output to csv
$headers = array("host", "domain", "domainX");
$output_file = "output.csv";

$fp = fopen($output_file, 'w');
fputcsv($fp, $headers);

foreach ( $urls as $url ) {
    $val = array($url["host"], $url["domain"], $url["domainX"]);
    fputcsv($fp, $val);
}

fclose($fp);

https://repl.it/@arvindDhakad/php-unique-urls-filter-DS