SHOW:
|
|
- or go back to the newest paste.
| 1 | - | function getWords(string $string, int $words): string |
| 1 | + | function getWords(string $string, int $words, int $offset = 0): string |
| 2 | {
| |
| 3 | $ret = ''; | |
| 4 | ||
| 5 | $insideTag = FALSE; | |
| 6 | $wCount = 0; | |
| 7 | $lastChar = NULL; | |
| 8 | $offset = max($offset, 0); | |
| 9 | - | for ($i = 0; ($i < \mb_strlen($string)) && ($wCount < $words); $i++) |
| 9 | + | |
| 10 | for ($i = 0; ($i < \mb_strlen($string)) && ($wCount < ($words + $offset)); $i++) | |
| 11 | {
| |
| 12 | $char = \mb_substr($string, $i, 1); | |
| 13 | ||
| 14 | if ($char === '<') | |
| 15 | {
| |
| 16 | $insideTag = TRUE; | |
| 17 | ||
| 18 | if (\ctype_alnum($lastChar)) | |
| 19 | {
| |
| 20 | $wCount++; | |
| 21 | } | |
| 22 | } | |
| 23 | else if ($char === '>') | |
| 24 | {
| |
| 25 | $insideTag = FALSE; | |
| 26 | } | |
| 27 | else if (!\ctype_alnum($char)) | |
| 28 | {
| |
| 29 | if (!$insideTag && \ctype_alnum($lastChar)) | |
| 30 | {
| |
| 31 | $wCount++; | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | $lastChar = $char; | |
| 36 | - | if (!$insideTag && ($char !== '>')) |
| 36 | + | |
| 37 | if (!$insideTag && ($char !== '>') && ($wCount >= $offset)) | |
| 38 | {
| |
| 39 | $ret .= $char; | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | $ret = trim($ret); | |
| 44 | ||
| 45 | while (!\ctype_alnum(\mb_substr($ret, 0, 1))) | |
| 46 | {
| |
| 47 | $ret = \mb_substr($ret, 1); | |
| 48 | } | |
| 49 | ||
| 50 | return $ret; | |
| 51 | } |