Return to Snippet

Revision: 18359
at September 29, 2009 05:41 by Ngoc


Initial Code
<?php
/**
* Trim a string to a given number of words
*
* @param $string
*   the original string
* @param $count
*   the word count
* @param $ellipsis
*   TRUE to add "..."
*   or use a string to define other character
* @param $node
*   provide the node and we'll set the $node->
*  
* @return
*   trimmed string with ellipsis added if it was truncated
*/
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 .= $ellipsis;
    }
    elseif ($ellipsis){
      $string .= '&hellip;';
    }
  }
  return $string;
}
?>

Initial URL
http://www.lullabot.com/articles/trim_a_string_to_a_given_word_count

Initial Description
Cut or trim a string by given word number

Initial Title
Trim A String To A Given Word Count

Initial Tags

                                

Initial Language
PHP