View Single Post
Old 09-07-2007, 11:21 PM   #1 (permalink)
bluesaga
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default [Tip] Limiting a strings length

Below is a handy function for shortening a string, where space is needed...

PHP Code:
function limitString($szInput$iLength=25$szAffix='...')
{
   if(
strlen($szInput) <= $iLength)
      return 
$szInput;

   
$iLength $iLength strlen($szAffix);

   return 
trim(substr($szInput0$iLength)) . $szAffix);

Example:
PHP Code:
$string "This is a long string that should be shortened using our great function";

print 
limitString($string10'...');
//Outputs "this is..." 

Last edited by bluesaga : 09-08-2007 at 12:37 AM.
bluesaga is offline  
Reply With Quote
The Following 2 Users Say Thank You to bluesaga For This Useful Post:
Alan @ CIT (01-10-2008), danielneri (01-10-2008)