mysterbx Posted June 1, 2008 Share Posted June 1, 2008 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 More sharing options...
chronister Posted June 1, 2008 Share Posted June 1, 2008 1. please use php tags so the code gets highlighted. 2. Please format the code so it is more easily read. 3. do the 2 items above and post back for help Link to comment https://forums.phpfreaks.com/topic/108211-group-links/#findComment-554669 Share on other sites More sharing options...
mysterbx Posted June 1, 2008 Author Share Posted June 1, 2008 <?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 More sharing options...
chronister Posted June 1, 2008 Share Posted June 1, 2008 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 More sharing options...
mysterbx Posted June 1, 2008 Author Share Posted June 1, 2008 Hi, and thats a lot for the help! The code was given to me by a member of this forum, i made some corrections, but it still didnt work for me the way i needed... good to know that it helped someone I will try this modified code, thanks again Link to comment https://forums.phpfreaks.com/topic/108211-group-links/#findComment-555103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.