...

View Full Version : How IPB Shortens URL's



pixencom
05-14-2008, 03:15 AM
I was curious to find out how IPB takes a long URL such as:


http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=BCR&q=metacafe&btnG=Search

and turns it into


http://www.google.com/search?hl=en&cli...amp;btnG=Search

with the three ... in the middle instead of the whole long URL. Does anyone know how?

Thanks.

EDIT:

Now that I look at it, Vbulletin does the same thing:

http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=BCR&q=metacafe&btnG=Search

GO ILLINI
05-14-2008, 05:10 AM
Its fairly simple actaully
step
1) if url var is less then x chars do nothing or
2) get first 15(?) chars into a variable.
3) get the last 10 chars
4) $first15 ... $last10 (inside a link to $orig_url)

Im sure that A) you can find a class, function that will do this for you already B) you can write it on your own(as its not very hard) or C) ask someone here to write it for you

Hope this leads you in the right direction,
-Adam

pixencom
05-14-2008, 05:57 AM
Thanks for your reply. Here is the function I made. This function shortens the url to 45 characters plus 7 characters for the divider.



<?php
function shortenurl($url){
$length = strlen($url);
if($length > 45){
$length = $length - 30;
$first = substr($url, 0, -$length);
$last = substr($url, -15);
$newurl = $first."[ ... ]".$last;
return $newurl;
}else{
return $url;
}
}

$longurl = "http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=BCR&q=metacafe&btnG=Search";
$shorturl = shortenurl($longurl);
echo"<a href=\"$longurl\">$shorturl</a>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum