Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Str::macro('sentences', function($value, $sentences = 2, $end = '...')
- {
- preg_match('/(.+?(?:(?<![\s.]\p{Lu})[.!?])){1,'.$sentences.'}/u', $value, $matches);
- if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
- return $value;
- }
- return rtrim($matches[0]).$end;
- });
- Str::macro('paragraphs', function($value, $paragraphs = 3, $end = '...')
- {
- preg_match('/(.+?(?:\s{2,})|\n*){1,'.$paragraphs.'}/um', $value, $matches);
- if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
- return $value;
- }
- return rtrim($matches[0]).$end;
- });
Add Comment
Please, Sign In to add comment