magebash Posted June 24, 2008 Share Posted June 24, 2008 I was making a messanging script and was wondering how to cut off a phrase and put... After it when it becomes more than a certain amount of characters long. If anyone can help it would be appreciated. Quote Link to comment Share on other sites More sharing options...
peranha Posted June 24, 2008 Share Posted June 24, 2008 <?php // Function to abbreviate number of characters. function truncate($string, $max_chars = 60) { if(strlen($string) > $max_chars) { $string = substr($string, 0, $max_chars).'...'; } return $string; ?> change 60 to the number of characters you want it to dispaly. Quote Link to comment Share on other sites More sharing options...
gijew Posted June 24, 2008 Share Posted June 24, 2008 <?php $text = 'whatever sentence'; if (strlen($text) > 5) { echo substr($text, 0, 50).'...'; } else { echo $text; } ?> EDIT: I like peranha's version better Quote Link to comment Share on other sites More sharing options...
magebash Posted June 24, 2008 Author Share Posted June 24, 2008 thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.