I have a string:
$str = 'Hello World, Welcome World, Bye World';
I want to cut above string into pieces. Each piece should be of 10 characters. If a word is going to be cut, then move that word to next line.
For Example:
$output = array();
$output[0] = 'Hello ';
$output[1] = 'World, ';
$output[2] = 'Welcome ';
$output[3] = 'World, Bye';
$output[4] = 'World';
Is there a shortest way without so many if else and loops.
Thanks
Use
wordwrap. By default it wraps whole words and does not cut them into pieces.If you want an array, explode afterwards: