RE: [PHP] Displaying first 100 characters of an article
| From: | Mark Maggelet | Date: | Wed, 04 Oct 2000 05:33:35 +0000 |
| Subject: | RE: [PHP] Displaying first 100 characters of an article | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18523@lists.php.net to get a copy of this message | ||
>or better still, make it into a function:
>
>
>function TrimString ($sString, $iLen = 125) {
> if (strlen($sString )<$iLen) {
> return ($sString);
> }
> else {
> return substr($sString,0,$iLen) . '...';
> }
>}
or maybe better still, make it so you don't get cut off in the middle
of a word:
function TrimString($sString,$iLen=125){
$retstring=strtok($sString," ")." ";
do {
if(!$tmp=strtok(" ")." ") break;
$retstring.=$tmp;
} while(strlen($retstring)<$iLen);
if(strlen($retstring)<strlen(trim($sString))) $retstring.="...";
return $retstring;
}