我想解析和排序一组链接 这是一个例子
表格代码
<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 google
和2 on yahoo
以及1 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
!!
任何想法怎么做〜谢谢
答案 0 :(得分: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
)
)
)
我认为您需要所有数据。
答案 1 :(得分: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>";
}