note 81165 deleted from function.explode by tularis
| From: | tularis@php.net | Date: | Mon, 27 Oct 2008 13:04:00 +0000 |
| Subject: | note 81165 deleted from function.explode by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145812@lists.php.net to get a copy of this message | ||
Note Submitter: contact at abhinavzone dot com
----
I wrote this function to shorten long names. Actually database has been designed to hold Full name
up to 20 characters but some user name had more than 20 chars.
/**
* Function abbreviate
* return abbreviate name
* This function is to abbreviate the name. If name is grater than specific length
* then it abbreviate the string
*
*
* Parameters:
* limit - The maximum character count
* string - The name string
*
* Returns:
* string - abbreviate string
*/
function abbreviate($limit, $string, $position=0)
{
$char_limit=$limit;
$string = trim($string);
$str_chunk = explode(' ', $string);
if(strlen($string) > $char_limit) {
if(sizeof($str_chunk) == ($position+1)){
return $string = substr($string, 0, $char_limit);
} else {
$str_chunk[$position] = substr($str_chunk[$position], 0, 1).'.';
return abbrivate($char_limit,implode(' ',$str_chunk), ++$position);
}
} else {
return $string;
}
}
//Usage
$name = 'Abhinav Varshney This Is My Name';
$char_limit=29;
echo abbreviate($char_limit, $name);