Q:

Split string with read more by word count

I need a split string with 2 divs, by the first div with 20 words and last div with rest of words to make read more link with javascript.  
At the moment I have only character count limit.  
How can I do word splitting? 
if ( $term && !  empty( $term->description ) ) {
            $first = substr($term->description, 0, 400);
            $rest = substr($term->description, 400);
            echo '<div class="term-description"><div class="first-letter">'.$first. '</div><div class="last-letter">'.$rest. '</div></div>';
        }

A:

This code does the trick:
<? php

function SplitStringToParts($sourceInput, &$first, &$rest, $countWordsInFirst = 20)
{
    $arr_exploded = explode(" ", $sourceInput);

    $arr_part1 = array_slice($arr_exploded, 0, $countWordsInFirst);
    $arr_part2 = array_slice($arr_exploded, $countWordsInFirst);

    $first = implode(" ",$arr_part1);
    $rest = implode(" ",$arr_part2);    
}

$str = "str1 str2 str3 str4 str5 str6 str7 str8 str9 str10 str11 str12 str13 str14 str15 str16 str17 str18 str19 str20 str21 str22 str23 str24";

SplitStringToParts($str,$first,$rest,20);

echo $first. "<br>";
echo $rest. "<br>";

Output is:
str1 str2 str3 str4 str5 str6 str7 str8 str9 str10 str11 str12 str13 str14 str15 str16 str17 str18 str19 str20
str21 str22 str23 str24

Use SplitStringToParts function.  In your case you should call it as:
SplitStringToParts($term->description, $first, $rest, 20);
After it $first, $rest will keep your result

