note 70517 deleted from function.substr by bobby
| From: | bobby@php.net | Date: | Thu, 19 Oct 2006 14:31:27 +0000 |
| Subject: | note 70517 deleted from function.substr by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118755@lists.php.net to get a copy of this message | ||
Note Submitter: digitally designed dot co dot uk
----
If you use substr to only get a part of a string it can cut off letters in the middle of a word...
This is my function to stop this.
<?
function fnShortenText($pstrText, $plintLength)
{
$larrSplitText = split(" ", $pstrText);
$i = 0;
$lstrMakeNewText = "";
do
{
if(strlen($lstrMakeNewText) + strlen($larrSplitText[$i]) < $plintLength)
{
$lstrMakeNewText .= $larrSplitText[$i];
$lstrMakeNewText .= " ";
$i++;
}
else
{
$lstrMakeNewText .= "...";
$i = count($larrSplitText);
}
}
while($i < count($larrSplitText));
return $lstrMakeNewText;
}
?>