Jump to content

group links


mysterbx

Recommended Posts

Hello,

 

I had an idea, and i dont know how to complete it...

The script should group all links from the same domain into one group from another to, another group, and so on..

 

My links:

http://dl.example1.com/1.zip
http://dl.example1.com/2.zip
http://dl.example2.com/1.zip
http://dl.example2.com/2.zip
http://dl.example1.com/1.zip
http://dl.example2.com/2.zip

 

Grouped links:

Download from dl.example1.com:
http://dl.example1.com/1.zip
http://dl.example1.com/2.zip

Download from dl.example2.com:
http://dl.example2.com/1.zip
http://dl.example2.com/2.zip

Download from dl.example3.com:
http://dl.example3.com/1.zip
http://dl.example3.com/2.zip

 

the script should look something like this [this one is buggy]

function get_domain ($url){ preg_match('@^(?:http://)?([^/]+)@i',$url, $matches); $host = $matches[1]; preg_match('/[^.]+\.[^.]+$/', $host, $matches); return $matches[0]; }
function downloadshow($dblink) {
$links=explode("\n",$dblink); foreach($links as $key => &$value){ $domain=get_domain($value); $domains[$domain].= "<a style='color:#7B7B7B;' target='_blank' href='$value'>".(strlen($value)>=70 ? substr($value,0,70)."...":$value)."</a><br>"; }
foreach($domains as $key => &$value){ if($key && $value) { $output .= "<br><b>Download from $key</b><br>$value"; } }
return $output;
}

Link to comment
https://forums.phpfreaks.com/topic/108211-group-links/
Share on other sites

<?php
function get_domain ($url){
preg_match('@^(?:http://)?([^/]+)@i',$url, $matches);
$host = $matches[1]; preg_match('/[^.]+\.[^.]+$/', $host, $matches);
return $matches[0];
}

function downloadshow($dblink) {
$links=explode("\n",$dblink);
foreach($links as $key => &$value){
$domain=get_domain($value); $domains[$domain].= "<a target='_blank' href='$value'>".(strlen($value)>=70 ? substr($value,0,70)."...":$value)."</a><br>";
}
foreach($domains as $key => &$value){ if($key && $value) {
$output .= "<br><b>Download from $key</b><br>$value";
}
}
return $output;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/108211-group-links/#findComment-554675
Share on other sites

Well mysterbx, your code works perfectly. You had a couple extra & symbols in there, but here is the corrected/modified code I used. Very nice code by the way. Since I suck at regex, I am going to hold on to this if ya don't mind in case I might need it in the future.

 

<?php
function get_domain ($url){
preg_match('@^(?:http://)?([^/]+)@i',$url, $matches);
$host = $matches[1]; preg_match('/[^.]+\.[^.]+$/', $host, $matches);
return $matches[0];
}

function downloadshow($dblink) {
$links=explode("\n",$dblink);
foreach($links as $key => $value){
$domain=get_domain($value); 
$domains[$domain].= "<a target='_blank' href='$value'>".(strlen($value)>=70 ? substr($value,0,70)."...":$value)."</a><br>";
}
foreach($domains as $key => $value){ if($key && $value) {
$output .= "<br><b>Download from $key</b><br>$value";
}
}
echo $output;
}
$links = 'http://dl.example1.com/1.zip
http://dl.example1.com/2.zip
http://dl.example2.com/1.zip
http://dl.example2.com/2.zip
http://dl.example3.com/1.zip
http://dl.example3.com/2.zip';

downloadshow($links);

?>

 

 

Here is the result I got.

 

Download from example1.com

http://dl.example1.com/1.zip

http://dl.example1.com/2.zip

 

Download from example2.com

http://dl.example2.com/1.zip

http://dl.example2.com/2.zip

 

Download from example3.com

http://dl.example3.com/1.zip

http://dl.example3.com/2.zip

Link to comment
https://forums.phpfreaks.com/topic/108211-group-links/#findComment-554682
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.