Never been to DZone Snippets 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

Truncate String by Words (See related posts)

This will take a phrase and truncate it at the word level
<?php

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;
}
?>

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


Click here to browse all 4858 code snippets

Related Posts