I’ve got a large string that I want to put in an array after each 50 words. I thought about using strsplit to cut, but realised that wont take the words in to consideration, just split when it gets to x char.
I’ve read about str_word_count but can’t work out how to put the two together.
What I’ve got at the moment is:
$outputArr = str_split($output, 250);
foreach($outputArr as $arOut){
echo $arOut;
echo "<br />";
}
But I want to substitute that to form each item of the array at 50 words instead of 250 characters.
Any help will be much appreciated.
Assuming that
str_word_countis sufficient for your needs¹, you can simply call it with1as the second parameter and then usearray_chunkto group the words in groups of 50:You now have an array of arrays; to join every 50 words together and make it an array of strings you can use
¹ Most probably it is not. If you want to get what most humans consider acceptable results when processing written language you will have to use
preg_splitwith some suitable regular expression instead.