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. Link to comment https://forums.phpfreaks.com/topic/111617-after-certain-amount-of-characters/ 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. Link to comment https://forums.phpfreaks.com/topic/111617-after-certain-amount-of-characters/#findComment-572946 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 Link to comment https://forums.phpfreaks.com/topic/111617-after-certain-amount-of-characters/#findComment-572947 Share on other sites More sharing options...
magebash Posted June 24, 2008 Author Share Posted June 24, 2008 thanks Link to comment https://forums.phpfreaks.com/topic/111617-after-certain-amount-of-characters/#findComment-573217 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.