Shorten URL Help

Discussion in 'PHP' started by MakeThatDollar, Aug 28, 2008.

  1. #1
    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.
     
    MakeThatDollar, Aug 28, 2008 IP
  2. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    ahowell, Aug 28, 2008 IP
  3. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #3
    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.
     
    MakeThatDollar, Aug 28, 2008 IP
  4. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?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
     
    rcadble, Aug 28, 2008 IP
  5. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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...
     
    ahowell, Aug 28, 2008 IP
  6. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #6
    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.
     
    MakeThatDollar, Aug 28, 2008 IP
  7. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    so cut them all in half...

    
    foreach ($result as $url) {
        echo cutUrl($url, round(strlen($url) / 2));
    }
    PHP:
     
    ahowell, Aug 28, 2008 IP
  8. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #8
    I'm not wanting to cut them all in half. I just want to trim the long ones and make them smaller.
     
    MakeThatDollar, Aug 28, 2008 IP
  9. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok, your a bit confusing =)

    You can accomplish this by:
    foreach ($result as &$url) {
        if (strlen($url) > $someLengthThatIsTooLong) {
            $url = cutUrl($url, $newLengthThatIsNotTooLong);
        }
    }
    PHP:
     
    ahowell, Aug 28, 2008 IP
  10. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #10
    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>&nbsp;&nbsp;&nbsp;</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. :)
     
    MakeThatDollar, Aug 28, 2008 IP
  11. oguz

    oguz Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thanks:)
    ...
     
    oguz, Aug 28, 2008 IP
  12. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Did you not even look at the one I showed you? It works perfectly.
     
    rcadble, Aug 28, 2008 IP
  13. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #13
    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?
     
    MakeThatDollar, Aug 28, 2008 IP
  14. rcadble

    rcadble Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    It's fixable.
     
    rcadble, Aug 28, 2008 IP
  15. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #15
    How do I do it? :confused:
     
    MakeThatDollar, Aug 28, 2008 IP