Is there a better way or better function to use to shorten URL's? function shrink_url( $link, $limit = 20, $dots = 3 ) { if (strlen($link) < 20) return $link; else return sprintf('%s%s%s', substr( $link, 0, $limit ), str_pad('.', $dots, '.' ), substr( $link, strlen( $link ) - 6 ) ); } PHP: For example, when the code above is implemented, it changes the following: djchristopherjames.com --> djchristopherjames.c...es.com parishiltonvideosonline.com --> parishiltonvideosonl...ne.com bayareadrivingschoolls.com --> bayareadrivingschool...ls.com fightdepression.info --> fightdepression.info...n.info As you can see, the URL it changes it into isn't correct. Hope someone can help. Thanks.
function cutUrl($string, $limit = 20) { $pad = '---'; $ext = substr($string, -4, 4); if (strlen($string) > ($limit - (strlen($pad) + strlen($ext))) && $limit > (strlen($pad) + strlen($ext))) { $string = substr($string, 0, ($limit - (strlen($pad) + strlen($ext)))) . $pad . $ext; } return $string; } $string = 'bayareadrivingschoolls.com'; echo cutUrl($string, 15); // bayaread---.com PHP:
Thanks, I tried that but still not working correctly. Here are a few examples that you can see its not right: wiiarcade.com --> wiiarcad---.com ps44free.com --> ps44free---.com Of course it worked for the longer ones but these shorter ones its messed up.
<?php function shrink_url($link, $limit = 20, $dots = 3) { return strlen($link) > $limit ? substr($link, 0, $limit - (strlen($link) - strpos($link, ".")) - 1) . str_pad('', $dots, ".", STR_PAD_LEFT) . substr($link, strpos($link, ".") - 1, (strlen($link) - strpos($link, ".")) + 1) : $link; } echo shrink_url("lksdjflkjsadlkfjlksdjflkasdjflk.com", 20, 3); echo "<br>" . shrink_url("djchristopherjames.com", 20, 3); ?> PHP: There are two examples at the bottom there, here's what the output is: lksdjflkjsadlkf...k.com djchristopherja...s.com
Why are you truncating the small url's? I can understand if it's really long. But why the short url's? what is the benefit? The code I posted will work just fine for what your asking. You'll have to call the function for each url, and you'll also have to pass a length value. If you want to truncate a 10 character url, that's strange, but works fine. echo cutUrl('wiiarcade.com', 10); // wii---.com PHP: the output string length will match the number you pass in...
I can't put the URL in the function and a number for each. It's pulling 500 names from a database and using the function to shorten the long ones, so therefore I can't specify the number for each, just like you did " 10 " for wiiarcade.
so cut them all in half... foreach ($result as $url) { echo cutUrl($url, round(strlen($url) / 2)); } PHP:
Ok, your a bit confusing =) You can accomplish this by: foreach ($result as &$url) { if (strlen($url) > $someLengthThatIsTooLong) { $url = cutUrl($url, $newLengthThatIsNotTooLong); } } PHP:
Sorry I'm confusing, but I thought I was pretty straight forward. Thanks for the new code but I'm not getting it right. I keep getting an error, "Warning: Invalid argument supplied for foreach()". Here's some code that may help: Right now I just have, $domshrink = $domain[domain]; And then: <td title=\"$domain[description]\" nowrap>" . ($domain['issite'] ? "<a href=\"http://www.dayonlinesolutions.com/go/$dom\" title=\"$domain[description]\" target=\"_blank\"><img src=\"./templates/images/site.png\" alt=\"Go to site for $domain[description]\" /></a> " : '') . "<a href=\"./go/$dom\" alt=\"Visit $domain[domain]\" title=\"Visit $domain[domain]\" target=\"_blank\"/><font color=black>$domshrink</font></a> </td> The results are shown here --> http://www.dayonlinesolutions.com/domains.php?sort=pagerank&soption=DESC <-- this type of URL is what I'm wanting.
Yeah must have missed that, sorry. I just added it. It's better but still shortening some that don't really need to be. For example: buy-electric-cars.com --> buy-electric-ca...s.com (replaced the R with 3 dots) prisonbreakforum.info --> prisonbreakfor...m.info (replaced the U with 3 dots) So in those cases, it actually increased the length of each. Anyway to fix that?