I have a string s1. I want to insert newline character for every 100 Maximum characters. The words should not be split.

example, $s1 = “word1 word2 word3 word4 word5…wordn”;

if the 100th character is in middle of the word then the newline character should be inserted at the end of the previous ending word.

suppose 100th character is o in word3 then the newline character should be inserted after word2.

Can someone help me in this?

i could have used wordwrap, sometimes the number of characters in the words are smaller and larger in different lines it does not look nice.


function excerptWrap($string, $desLength, $endChars = '...', $before = '1', $offChar = ' ')
				case 0:
				$partialString = substr($string, $desLength, strlen($string));
				$endPosition = strpos($partialString, $offChar);
				return substr($string, 0, $desLength+$endPosition).$endChars;
				case 1:
				$string = substr($string, 0, $desLength);
				$endPosition = strrpos($string, $offChar);
				return $string = substr($string, 0, $endPosition).$endChars;



echo excerptWrap($s1, 100); 

It assumes space is the char to cut off, and before means it will cut off before the limit if there’s a word in between.