Page 1 of 1

string manipulation question

Posted: Sun May 21, 2006 7:23 pm
by Milan
Woa! My third question today and i do know i am getting borring :P

but PHP is so much fun!

I would like to know how to trim the variable to display only the first 200 characters of description? :roll:

thanks a lot guys!

Posted: Sun May 21, 2006 7:31 pm
by litebearer
This will truncate a string to a chosen length, make sure it does NOT chop off in the middle of a word, and add trailing dots.

Code: Select all

<?PHP

function truncate_string($details,$max)
{
    if(strlen($details)>$max)
    {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details." ..... ";
    }
    return $details;
}

$text = truncate_string("hello there. This is a long string",19);

?>
Lite...

thanks!

Posted: Sun May 21, 2006 7:35 pm
by Milan
Thanks a lot!