Advertisement
Guest User

Untitled

a guest
May 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. <?php
  2. function character_limiter($str, $n = 500, $end_char = '&#8230;')
  3. {
  4. if (strlen($str) < $n)
  5. {
  6. return $str;
  7. }
  8.  
  9. $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
  10.  
  11. if (strlen($str) <= $n)
  12. {
  13. return $str;
  14. }
  15.  
  16. $out = "";
  17. foreach (explode(' ', trim($str)) as $val)
  18. {
  19. $out .= $val.' ';
  20.  
  21. if (strlen($out) >= $n)
  22. {
  23. $out = trim($out);
  24. return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
  25. }
  26. }
  27. }
  28.  
  29. $string = 'one two three four';
  30. $limited = character_limiter($string, 7, '');
  31. $string = explode($limited, $string);
  32. $string[0] = $limited;
  33. print_r($string);
  34. ?>
  35.  
  36.  
  37. =========================
  38.  
  39.  
  40. Array ( [0] => one two [1] => three four )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement