PDA

View Full Version : Limit ammount of characters with php?



Tom-743
26-07-2008, 07:50 PM
Im trying to limit a string to 10 characters, but i can't work out how to do it. Ive tried string pad but it can only be used to make the string bigger;

$shorten = "hihihihihihihihihihiihhihihihihihihih";
$shorten = str_pad ( $shorten, 10 );
echo $shorten

That just echos "hihihihihihihihihihiihhihihihihihihih". Is there any function to shrink a string?

Invent
26-07-2008, 08:23 PM
Hi,

You could use the function substr.

For example:




$wat = "thisstringisover10characters";
$wat = substr( $wat, 0, 10 );

echo $wat; // outputs: thisstring



Thanks,
Invent

Tom-743
26-07-2008, 08:40 PM
Thats exactly what i was looking for, Thanks :) I will +rep you when i get my other account back.

Invent
26-07-2008, 09:54 PM
Hi,

Good to hear thats what you needed :)

Thanks,
Invent

Want to hide these adverts? Register an account for free!