Limit a string to 13 charcters...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ggwarpig
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 12:47 pm

Limit a string to 13 charcters...

Post 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!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Limit a string to 13 charcters...

Post by jackpf »

Code: Select all

if(strlen($string) > 13)
$string = substr($string, 0, 10);
like that?
ggwarpig
Forum Newbie
Posts: 7
Joined: Wed Sep 09, 2009 12:47 pm

Re: Limit a string to 13 charcters...

Post 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;
}
 
Post Reply