Search code examples
phpstringstrsplit

PHP Using str_word_count with strsplit to form array after x words


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.


Solution

  • Assuming that str_word_count is sufficient for your needs¹, you can simply call it with 1 as the second parameter and then use array_chunk to group the words in groups of 50:

    $words = str_word_count($string, 1);
    $chunks = array_chunk($words, 50);
    

    You now have an array of arrays; to join every 50 words together and make it an array of strings you can use

    foreach ($chunks as &$chunk) { // important: iterate by reference!
        $chunk = implode(' ', $chunk);
    }
    

    ¹ 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_split with some suitable regular expression instead.