When I use substr($string,0,100), it gives first 100 characters. Sometimes it left the last word incomplete. That looks odd. Can I do limit by word rather than char?
When I use substr($string,0,100) , it gives first 100 characters. Sometimes it left the
Share
If you just count the words the resulting sting could still be very long as a single “word” might have 30 characters or more. I would suggest instead truncating the text to 100 characters, except if this causes a word to be truncated then you should also remove the truncated part of the word. This is covered by this related question:
How to Truncate a string in PHP to the word closest to a certain number of characters?
Using wordwrap
This is a modified versions of the answer here. if the input text could be very long you can add this line before the call to wordwrap to avoid wordwrap having to parse the entire text:
Using a regular expression (Source)