No announcement yet.

URL shortened...

  • Filter
  • Time
  • Show
Clear All
new posts

  • URL shortened...


    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 ( .. like that with the 3 dots.


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


    • #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.


      • #4
        There probably isn't a script to do this. It's as easy as ->
        PHP Code:
        '<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.


        • #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
 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 =)



          • #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?


            • #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.


              • #8
                Okay, I coded it, and it's rather simple... Took less than 2 minutes
                PHP Code:

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

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


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