-6

我想解析和排序链接集这里是例子

表格代码

<form name="frm" method="post" action="test.php">
<textarea id="url" name="url"></textarea>
<input type="Submit" name="Submit" value="Submit"/>
</form>

我将每行提交以下链接

http://www.google.com/moo
http://www.yahoo.com/boo
http://www.google.com/zee
http://www.bing.com/kee
http://www.yahoo.com/foo

这是2 on google2 on yahoo1 on bing链接

test.php代码如下(表单发送提交的地方)

<?PHP
$url=$_POST['url'];
$url=nl2br($url);
$url=explode("<br />",$url);
foreach ($url as $value ){
$encrypt = md5($value);
echo $encrypt . "<br>";
}
?>

输出将如下

8ec5ec689ab94e5df9d89cea624e7e5e //google.com/moo
b165a8209254d205a2950f23214125ec //yahoo.com/boo
fc8d853005d21a7fc8abb06aba0756fb //google.com/zee
f691aab0c39f288f503ae61d3cc3b5b4 //bing.com/kee
8f55ee4d5227f87ec1316e0fa6c61e3b //yahoo.com/foo

现在我的问题

我想解析主机以了解哪个主机google,yahoo and bing并对其进行排序,然后将结果完全显示如下

google.com // parsed and sorted
fc8d853005d21a7fc8abb06aba0756fb //google.com/zee
8ec5ec689ab94e5df9d89cea624e7e5e //google.com/moo

yahoo.com // parsed and sorted
b165a8209254d205a2950f23214125ec //yahoo.com/boo
8f55ee4d5227f87ec1316e0fa6c61e3b //yahoo.com/foo

bing.com // parsed and sorted
f691aab0c39f288f503ae61d3cc3b5b4 //bing.com/kee

所以换句话说,我的意思是我想对他们的主机上的链接进行排序,然后显示在其解析的主机下对每个组进行排序的结果。

我想我需要做很多foreach

知道怎么做~谢谢

4

2 回答 2

2

代码:

$urlByHost = array();
foreach (explode("\n", $_POST['url']) as $value) {
    $parse = parse_url($value);
    $urlByHost[$parse['host']][] = array(
        'url' => $value,
        'parse' => $parse,
        'md5' => md5($value),
    );
}
asort($urlByHost);

print_r($urlByHost);

输出:

Array
(
    [www.bing.com] => Array
        (
            [0] => Array
                (
                    [url] => http://www.bing.com/kee
                    [parse] => Array
                        (
                            [scheme] => http
                            [host] => www.bing.com
                            [path] => /kee_
                        )

                    [md5] => e69d3a5bb987448e30ec8559c3634caf
                )

        )

    [www.google.com] => Array
        (
            [0] => Array
                (
                    [url] => http://www.google.com/moo
                    [parse] => Array
                        (
                            [scheme] => http
                            [host] => www.google.com
                            [path] => /moo_
                        )

                    [md5] => f98f559bb167acb6413b55c6c7b7255a
                )

            [1] => Array
                (
                    [url] => http://www.google.com/zee
                    [parse] => Array
                        (
                            [scheme] => http
                            [host] => www.google.com
                            [path] => /zee_
                        )

                    [md5] => 717b78e3db16982d77dde33991c4db70
                )

        )

    [www.yahoo.com] => Array
        (
            [0] => Array
                (
                    [url] => http://www.yahoo.com/boo
                    [parse] => Array
                        (
                            [scheme] => http
                            [host] => www.yahoo.com
                            [path] => /boo_
                        )

                    [md5] => b237c6cd567aaef629d55ae53f52dc49
                )

            [1] => Array
                (
                    [url] => http://www.yahoo.com/foo
                    [parse] => Array
                        (
                            [scheme] => http
                            [host] => www.yahoo.com
                            [path] => /foo
                        )

                    [md5] => bd34d7a6adf909e4ce355b038e8d206c
                )

        )

)

我认为有你需要的所有数据。

于 2012-10-01T18:18:57.830 回答
1

您可以通过提取域并将其用作具有加密值的数组的索引来做到这一点,如下所示:

$url=$_POST['url'];
$url=nl2br($url);
$url=explode("<br />",$url);
$urls = array();
foreach ($url as $value ){
   $arr = explode('www.',$value);
   $encrypt = md5($value);
   $urls[$arr[1]][]= $encrypt; //this line now fixed, had an error
}
foreach($urls as $key => &$val) {
   echo $key . "<br>";
   foreach($val as $v) {
      echo $v . "<br>";
   }
   echo "<br>";
}
于 2012-10-01T18:21:31.470 回答