Esempio n. 1
0
 /**
  * 
  * Limits the number of characters in a string.
  * @param string $string
  * @param uint $character_limit
  * maximum number of characters to return, inclusive of any added ellipsis
  * NOTE: this is characters, not bytes (think UTF8). Be generous with columns
  * @param optional array
  * if $options['append_ellipsis'] is set, append that string to the end
  * of strings that have been truncated
  * @return string
  * new string containing only characters up to the limit
  * Suitable when a word count limit is not enough (because words are
  * sometimes unreasonably long).
  * Tries to preserve word boundaries, but not too hard, as very long words can
  * create problems of their own.
  */
 public static function limitCharacters($s, $length, $options = array())
 {
     $ellipsis = "";
     if (isset($options['append_ellipsis']) && $options['append_ellipsis']) {
         $ellipsis = "...";
     }
     if ($length < 12) {
         // Not designed to be elegant below this length
         return aString::substr($s, 0, $length);
     }
     if (aString::strlen($s) > $length) {
         $s = aString::substr($s, 0, $length - aString::strlen($ellipsis));
         $slength = aString::strlen($s);
         for ($i = 1; $i <= 10; $i++) {
             $c = aString::substr($s, $slength - $i, 1);
             if ($c === ' ' || $c === '\\t' || $c === '\\r' || $c === '\\n') {
                 return aString::substr($s, 0, $slength) . $ellipsis;
             }
         }
         return $s . $ellipsis;
     }
     return $s;
 }