function word_trim($string, $count, $ellipsis = FALSE)
{
    $words = explode(' ', $string);
    if (count($words) > $count) {
        array_splice($words, $count);
        $string = implode(' ', $words);
        if (is_string($ellipsis)) {
            $string = restoreTags($string);
            $string .= $ellipsis;
        } elseif ($ellipsis) {
            $string = restoreTags($string);
            $string .= '…';
        }
    }
    return restoreTags($string);
}
Example #2
0
function truncateWords($input, $numWords = 50, $restoreTags = 'auto', $end = '...')
{
    if (str_word_count($input) <= $numWords) {
        return $input;
    }
    if (preg_match("/(\\S+\\s*){0,{$numWords}}/", $input, $matches)) {
        $shortDesc = trim($matches[0]);
        switch ($restoreTags) {
            case 'auto':
                $shortDesc = restoreTags($shortDesc);
                break;
            case 'purifier':
                $purifier = new CHtmlPurifier();
                $shortDesc = $purifier->purify($shortDesc);
                break;
        }
    }
    return $shortDesc . $end;
}