axxe16

truncate_str tronca le stringhe conservando le parole

Apr 11th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.39 KB | None | 0 0
  1. //tronca le stringhe conservando le parole
  2. //#abstract #stringhe #truncate
  3. //$text -> stringa da torncare
  4. //$length -> numero di caratteri
  5. //$more -> parte finale
  6. function truncate_str($text, $length, $more = '...') {
  7.    $length = abs((int)$length);
  8.    if(strlen($text) > $length) {
  9.       $text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1' . $more, $text);
  10.    }
  11.    return($text);
  12. }
Advertisement
Add Comment
Please, Sign In to add comment