note 12634 deleted from ref.strings by maxim
| From: | maxim@php.net | Date: | Tue, 12 Nov 2002 16:19:45 +0000 |
| Subject: | note 12634 deleted from ref.strings by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39396@lists.php.net to get a copy of this message | ||
To shorten a string to a certain number of characters, replacing surplus chars with a shortening
symbol such as "[...]", use the following simple function:
========= CUT ==========
function shorten_string($text, $length, $symbol = "[...]")
{
$length_text = strlen($text);
$length_symbol = strlen($symbol);
if($length_text <= $length || $length_text <= $length_symbol || $length <=
$length_symbol)
return($text);
else
return(substr($text, 0, $length - $length_symbol) . $symbol);
}
========= CUT ==========
The parameter $text ist the string to be shortened. $length is the maximum number of characters in
the new string. $symbol is the shortening symbol (default is "[...]"). If shortening would
make no sense, the original string is returned.