URL shortened...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • R1Swedie
    Senior Member
    • Oct 2003
    • 128
    • 2.3.0

    URL shortened...

    Hey

    I see vBulletin have shortens URL's that are longer than a certain number of characters. Can you possibly share with me what the script is to do this?

    Currently I just have a script to turn URL's into HREF's, but since some URL's are scary long and messed up the layout I wish to shorten the URL down, just like vBulletin does (http://www.sporthoj.com/forum/for...alongurl) .. like that with the 3 dots.

    Thanks!
  • R1Swedie
    Senior Member
    • Oct 2003
    • 128
    • 2.3.0

    #2
    or you don't reveal such big secrets? :/

    Comment

    • noppid
      Senior Member
      • May 2003
      • 625
      • 2.3.2

      #3
      Buffer the URL and for the href echo the whole url, for the url text echo the var shortened plus the 3 dots.
      Computer Help Forum
      An informed rider makes their first destination the motorcycle forum at rider info.

      Comment

      • rebby
        Senior Member
        • Mar 2001
        • 220
        • 2.3.3

        #4
        There probably isn't a script to do this. It's as easy as ->
        PHP Code:
        $url='http://www.vbulletin.com/forum/showthread.php?t=91514';
        $numchars=15;
        echo 
        '<a href="' $url '">' substr($url,0,$numchars) . '...</a>'
        If you don't want the "..."'s displayed when the string length is <= $numchars, just add some if logic using 'strlen($url)'.

        That's all there is to it.

        Comment

        • R1Swedie
          Senior Member
          • Oct 2003
          • 128
          • 2.3.0

          #5
          dammit! i've asked this question in like.. 3 different forums now and I keep getting misunderstod!

          I know that way of doing it, that is simple! But, what I want is...

          Let's say I have this piece of text in a $string:

          **************
          I rock, I'm better than you, therefore, visit
          http://www.isuck.com/sdf/sd/fsad/f/s...f/sdf/as/d.jpg so that you know where your home is.. yada yadyaydaydaydayd
          **************

          Now I want a script to pick up that URL ... and since it's so DAMN LONG, i want it shortened down to only 30 characters total and end it with "...". But of course only if it is larger than 30 characters.

          Get me now? I hope =)

          Thanks

          Comment

          • Keith
            New Member
            • Feb 2001
            • 4

            #6
            PHP Code:
            $url 'http://www.domain.tld/directory/file.ext';
            $numchars '30';
             
            if (
            strlen($url) > $numchars) echo '<a href="' $url '">' substr($url0$numchars) . '...</a>';
            else echo 
            '<a href="' $url '">' $url '</a>'
            like that?

            Comment

            • digitok
              New Member
              • Apr 2003
              • 11
              • 3.0.0 'Gold'

              #7
              vBulletin's URL shortener seems to work differently... It doesn't just do a simple "if it's more than 30 chars, truncate it and add ..." It adds the .'s in certain places in the string, then displays the end of the url... I don't know exactly how it's done...

              I'm sure it's simple logic... eg, if the string is more than 50 chars, display the first 35, then 3 dots, then the last 15, or something like that... (I counted and it displays 35, then 3 dots, then 15...) I'll make a small piece of code and post it here in a tick.

              Comment

              • digitok
                New Member
                • Apr 2003
                • 11
                • 3.0.0 'Gold'

                #8
                Okay, I coded it, and it's rather simple... Took less than 2 minutes
                PHP Code:
                <?
                    $url 
                'someurl';

                    if (
                strlen($url) > 50)
                        echo 
                '<a href="',$url,'">',substr($url,0,35),'...',substr($url,-15),'</a>';

                    else
                        echo 
                '<a href="',$url,'">',$url,'</a>';
                ?>

                Comment

                widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                Working...