Quick Search:
 
 PHP Code: Truncate Links Over X Chars long Jump to:  
Category: >> PHP Code >> Truncate Links Over X Chars long  

<< lastnext >>

Snippet Name: Truncate Links Over X Chars long

Description: Truncate URL if longer than 'X' characters (add http:// or ftp:// if missing)

Also see:
» Neatly trim a string to a set number o...
» Search array elements for a substring
» Add a period at the end of a string if...
» Count capital letters in a string
» Search All Words Of A String In mySQL
» Remove Extra Spaces From A String
» Get Query String with Javascript
» Create string by formatting an amount ...
» Search PL/SQL for a string/ key value
» INSTR (InString)
» SUBSTR (SubString)
» Generate unique strings of random numb...
» String Functions: REVERSE
» String Functions: LENGTH
» String Functions: INSTR
» String Functions: CONCAT
» String Functions: CHAR
» String Functions: INITCAP
» String Functions: LOWER
» String Functions: UPPER
» Sort comma separated string
» String to Date
» Generate random string
» De-Pluralize a String
» Add string to field
» Column copy with string replacement
» Extract part of a string
» Finding a substring in a string
» Make comma-separated list of strings
» Function For String Handling

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?PHP
 
FUNCTION handle_url_tag($url, $link = ''){
     GLOBAL $FORUM_user;
 
     // maximum link length
     $MAXLEN = 50;
 
     // if too long, clip it to how many chars?
     $CLIPTO = 45;
 
     $full_url = STR_REPLACE(ARRAY(' ', '\'', '`', '"'), array('%20', '', '', ''), $url);
     if (strpos($url, 'www.') === 0)               // If it starts with www, we add http://
          $full_url = 'http://'.$full_url;
     else if (strpos($url, 'ftp.') === 0)     // Else if it starts with ftp, we add ftp://
          $full_url = 'ftp://'.$full_url;
     else if (!preg_match('#^([a-z0-9]{3,6})://#', $url, $bah))      // Else if it doesn't start with abcdef://, we add http://
          $full_url = 'http://'.$full_url;
 
     // Ok, not very pretty :-)
     $link = ($link == '' || $link == $url) ? ((strlen($url) > $MAXLEN) ? substr($url, 0 , $CLIPTO).' … '.substr($url, -10) : $url) : stripslashes($link);
 
     return '<a href="'.$full_url.'">'.$link.'</a>';
}
 
?>


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org