PDA

View Full Version : Chopping characters off a string


cyphix
08-30-2004, 02:39 PM
Hey guys.. if I am checking the string length of a variable & I find that the variable is too long; how can I cut any characters off after a certain amount?

Say if the returned string is: 123456789

The max characters I want shown is 3..

So in the above example I would like to return 123

Thanks!

boeing747fp
08-30-2004, 02:43 PM
if(strlen($yourvariable) >3){
substr($yourvariable,0,3);
}

^if you want it to do a ... after the strings that have been shortened, just add ."..." to the end of the substr() function

Nightfire
08-30-2004, 02:49 PM
<?php
$str = '0123456789';
echo substr($str,0,3);
?>

<edit>Guess I shouldn't get so distracted :p Forgot I was replying to this until a minute a go</edit>

cyphix
08-30-2004, 03:41 PM
Thanks guys! :thumbsup: