Help with truncating links..

Discussion in 'PHP' started by MakeThatDollar, May 13, 2007.

  1. #1
    MakeThatDollar, May 13, 2007 IP
  2. nicknick

    nicknick Peon

    Messages:
    429
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can probably use mod_rewrite to change them to whatever you want. But I'm sorry, I don't know the exact code needed. A user here called Nintendo is really good with that type of stuff. Try posting again with a mod_rewrite thread and see if you can get someone to tell you how to do it exactly.

    This assumes you are on a unix server of course.
     
    nicknick, May 13, 2007 IP
  3. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #3
    Yeah thanks for your post, and yeah I'm on a Linux server.
     
    MakeThatDollar, May 13, 2007 IP
  4. nicknick

    nicknick Peon

    Messages:
    429
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nicknick, May 13, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    
    <?php
    
    $url = "http://forums.digitalpoint.com/newthread.php?do=newthread&f=37";
    
    $hyperlink = "<a href=\"$url\">$url</a>";
    
    function shrink_url( $link, $limit = 40, $dots = 5 )
    {
    	return sprintf('%s%s%s', substr( $link, 0, $limit ), str_pad('.', $dots, '.' ), substr( $link, strlen( $link ) - 4 ) );	
    }
    function format_hyperlink( $hyperlink )
    {
     	if(!preg_match('~(>(.*?[^<])<)~', $hyperlink, $url )):
     		return false;
     	elseif( $url[1] ):
     		return str_replace( $url[1], shrink_url( $url[1] ), $hyperlink );		
     	endif;	
    }
    echo "Shrunk Url<br />\r\n";
    echo shrink_url( $url ) . "\r\n";
    echo "Shrunk hyperlink<br/>\r\n";
    echo format_hyperlink( $hyperlink ) . "\r\n";
    
    PHP:
     
    krakjoe, May 13, 2007 IP
    MakeThatDollar likes this.
  6. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    What forums you are using if phpbb then there is a mod available to change the topic url.
     
    Subikar, May 13, 2007 IP
  7. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #7
    krakjoe, that code is working great. One issue I'm having though. It does it to a URL that is real short too though. How can I only get it to truncate the URL if it's over 40 characters?

    For example, the link http://wiifamily.net is shown as http://wiifamily.net...ly.net instead.

    Just so you know, I modified $dots = 5 to $dots = 3 and $link, strlen( $link ) - 4 to $link, strlen( $link ) - 6.

    Awaiting your response.

    Thanks again so far!!
     
    MakeThatDollar, May 14, 2007 IP
  8. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #8
    MakeThatDollar, May 14, 2007 IP