Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Word Chopper (See related posts)

// The following will limit a string to $max_words words
// usage: $var = word_chop($str(string), $max_words(limit of words, default: 15));


function word_chop($str, $max_words = 15) {
        $e = explode(' ', $str);
        $w = count($e);
        if($w > $max_words) {
                $str = '';
                for($i=0;$i<$max_words;$i++) {
                        $str .= ' '.$e[$i];
                }
        $str .= '...';
        }
        return($str);
}


You need to create an account or log in to post comments to this site.


Related Posts