Page 1 of 1

Limit a string to 13 charcters...

Posted: Sat Sep 12, 2009 3:51 pm
by ggwarpig
Can someone help me with a simple php function?

I have an area that will only fit 13 characters.
So if the string I want to put in there is longer than 13 characters I want to chop it down to 10 and add ...

Example:

Hello My Name Is Greg ->becomes-> Hello My N...

Thanks!

Re: Limit a string to 13 charcters...

Posted: Sat Sep 12, 2009 4:08 pm
by jackpf

Code: Select all

if(strlen($string) > 13)
$string = substr($string, 0, 10);
like that?

Re: Limit a string to 13 charcters...

Posted: Sat Sep 12, 2009 5:02 pm
by ggwarpig
ahhhh substr... perfect. Thank you!
Here is my final function (updated):

Code: Select all

function MakeItFit($string,$chars){
    if(strlen($string) > $chars){
        $string = substr($string, 0, $chars -3);
        $string .= '...';
    }
return $string;
}