Tutorial Truncate String by Words

Technique #1 function trunc($phrase, $max_words) { $phrase_array = explode(' ',$phrase); if(count($phrase_array) > $max_wor...

Technique #1
function trunc($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;
}
?>
Technique #2
function limit_words($words, $limit, $append = ' …') {
// Add 1 to the specified limit becuase arrays

Hot in Week

Popular

Archive

item