Rifts Posted November 9, 2010 Share Posted November 9, 2010 Hey quick question on my homepage there is a featured content section, which is populated from my DB some of the posts are really long so I would like to limit the amount of text. how can I do this? also is there a way to make sure if does not cut a word in half? thanks Link to comment https://forums.phpfreaks.com/topic/218207-possible-to-limit-amount-of-words/ Share on other sites More sharing options...
Rifts Posted November 9, 2010 Author Share Posted November 9, 2010 figured it out.. in case anyone else needs this <?php function limit_words($string, $word_limit) { $words = explode(" ",$string); return implode(" ",array_splice($words,0,$word_limit)); } # Example Usage $content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; echo limit_words($content,20); ?> Link to comment https://forums.phpfreaks.com/topic/218207-possible-to-limit-amount-of-words/#findComment-1132283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.