PHP script to display short URL

Long URL sometimes will break your page design.

I have been using this function quite a while on my forum site, so there is no more problem with long URL pasted by users:

function shortu($url,$chr_limit = 30,$add = ‘…’) {
$patterns=array(”!(>http:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*<)!e","!(>https:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*<)!e","!(>ftp:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#]*<)!e");
$replacements=array("''.(strlen('\\1')>=$chr_limit*2 ? substr(’\\1′,0,$chr_limit).’$add’.substr(’\\1′,0-$chr_limit,-1).’<':'\\1').''","''.(strlen('\\1')>=$chr_limit*2 ? substr(’\\1′,0,$chr_limit).’$add’.substr(’\\1′,0-$chr_limit,-1).’<':'\\1').''","''.(strlen('\\1')>=$chr_limit*2 ? substr(’\\1′,0,$chr_limit).’$add’.substr(’\\1′,0-$chr_limit,-1).’<':'\\1').''");
return preg_replace($patterns,$replacements, $url);
}

Posted February 17th, 2006 by mightyhitter under PHP

 

Leave a Reply