Advertisement
Guest User

Untitled

a guest
May 26th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. /**
  2. * Character Limiter
  3. *
  4. * Limits the string based on the character count. Preserves complete words
  5. * so the character count may not be exactly as specified.
  6. *
  7. * @access public
  8. * @param string
  9. * @param integer
  10. * @param string the end character. Usually an ellipsis
  11. * @return string
  12. */
  13. if ( ! function_exists('character_limiter'))
  14. {
  15. function character_limiter($str, $n = 500, $end_char = '…')
  16. {
  17. if (strlen($str) < $n)
  18. {
  19. return $str;
  20. }
  21.  
  22. $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
  23.  
  24. if (strlen($str) <= $n)
  25. {
  26. return $str;
  27. }
  28.  
  29. $out = "";
  30. foreach (explode(' ', trim($str)) as $val)
  31. {
  32. $out .= $val.' ';
  33.  
  34. if (strlen($out) >= $n)
  35. {
  36. $out = trim($out);
  37. return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
  38. }
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement