Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. <?php
  2.  
  3. Str::macro('sentences', function($value, $sentences = 2, $end = '...')
  4. {
  5. preg_match('/(.+?(?:(?<![\s.]\p{Lu})[.!?])){1,'.$sentences.'}/u', $value, $matches);
  6.  
  7. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  8. return $value;
  9. }
  10.  
  11. return rtrim($matches[0]).$end;
  12. });
  13.  
  14. Str::macro('paragraphs', function($value, $paragraphs = 3, $end = '...')
  15. {
  16. preg_match('/(.+?(?:\s{2,})|\n*){1,'.$paragraphs.'}/um', $value, $matches);
  17.  
  18. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  19. return $value;
  20. }
  21.  
  22. return rtrim($matches[0]).$end;
  23. });
Add Comment
Please, Sign In to add comment