Saturday, February 9, 2013

PHP Function | Truncate String by words

This function have 2 parameters $phrase and $words, you pass your content variable that contain full text in $phrase variable, and you put number of words that you want see as output.


function trunc_phrase($phrase, $max_words)
{
$phrase_array = explode(' ',$phrase);
if(count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(' ',array_slice($phrase_array, 0,$max_words)).'...');
return $phrase;
}

Disqus Comments