SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member Lambda's Avatar
    Join Date
    Sep 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP URL Shortening

    How can i shorten sentences or URLs like Vbulletin does?

    http://www.sitepoint.com/f[...]wthread.php?t=450080


    See the '...'?


    Edit: this forum is not confugered to do that, but for example, i have this sentence:


    Hello, my name is Joe!

    And the result i want is:

    Hello, my [...] Joe!

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function displayLink($url,$length=40) {
      return 
    "<a href=\"$url\">" substr($url,0,$length) . "...</a>";
    }

    $myUrl 'http://www.sitepoint.com/forums/showthread.php?t=450080';

    echo 
    shortLink($myUrl); 
    That'll shorten the length to 40 characters followed by 3 dots. If you want a length aside from 40 just do displayLink($myUrl,20);

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh you want to shorten it in the middle?

    Well same basic approach. Just get the first 20 characters, display ..., display the last 20 characters.

    Or whatever.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    Non-Member Lambda's Avatar
    Join Date
    Sep 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •