View Full Version : URL shortened...
R1Swedie
Wed 7th Jan '04, 7:54pm
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... (http://www.sporthoj.com/forum/for...c)alongurl) .. like that with the 3 dots.
Thanks!
R1Swedie
Fri 9th Jan '04, 12:28pm
or you don't reveal such big secrets? :/
noppid
Fri 9th Jan '04, 1:00pm
Buffer the URL and for the href echo the whole url, for the url text echo the var shortened plus the 3 dots.
rebby
Fri 9th Jan '04, 2:26pm
There probably isn't a script to do this. It's as easy as ->
$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.
R1Swedie
Fri 9th Jan '04, 5:11pm
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/sadf/a//sd/fs/df/sa/df/a/sd/f/asd/fa/sdf/asd/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
Keith
Sun 25th Jan '04, 8:34pm
$url = 'http://www.domain.tld/directory/file.ext';
$numchars = '30';
if (strlen($url) > $numchars) echo '<a href="' . $url . '">' . substr($url, 0, $numchars) . '...</a>';
else echo '<a href="' . $url . '">' . $url . '</a>';
like that?
digitok
Fri 30th Jan '04, 6:11am
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.
digitok
Fri 30th Jan '04, 6:16am
Okay, I coded it, and it's rather simple... Took less than 2 minutes ;)
<?
$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>';
?>
vBulletin® v3.8.0 Beta 3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.