Home >> Snippet List >> Snippet

Name
Truncate URLs for clean appearance.
Description
Makes nicer looking URLs quickly and easily Prevent stretching a page out of shape by clipping off the visible portion of the URL
PHP Snippet
<?PHP
 
FUNCTION shorturl($url){
    $length = STRLEN($url);
    IF($length > 45){
        $length = $length - 30;
        $first = SUBSTR($url, 0, -$length);
        $last = SUBSTR($url, -15);
        $new = $first."[ ... ]".$last;
        RETURN $new;
    }ELSE{
        RETURN $url;
    }
}
 
?>
 
// example usage
 
<?PHP
 
$longurl= "http://www.google.com/search?q=psoug+substring&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:tr:official&client=firefox-a";
$shorturl = shorturl($longurl);
PRINT "<a href='$longurl'>$shorturl</a>";
 
?>