PHP truncate on word ending and add elipsis

<?php
/**
 * Truncates a string to a certain char length, stopping on a word if not specified otherwise.
 */
function Truncate($string, $length, $stopanywhere = FALSE) {
  if (strlen($string) > $length) {
    //limit hit!
    $string = substr($string, 0, ($length - 3));
    if ($stopanywhere) {
      //stop anywhere
      $string .= '...';
    } else {
      //stop on a word.
      $string = substr($string, 0, strrpos($string, ' ')) . '...';
    }
  }
  return $string;
}
 
?>

Article Type

General