note 81220 deleted from function.explode by tularis
| From: | tularis@php.net | Date: | Mon, 27 Oct 2008 13:03:48 +0000 |
| Subject: | note 81220 deleted from function.explode by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145811@lists.php.net to get a copy of this message | ||
Note Submitter: contact at abhinavzone dot com
----
Hey a small fix in my code
<?php
/**
* 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 - abbreviated 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 abbreviate($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);
// Output::
// A. Varshney This Is My Name
?>