Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getWords(string $string, int $words, int $offset = 0): string
- {
- $ret = '';
- $insideTag = FALSE;
- $wCount = 0;
- $lastChar = NULL;
- $offset = max($offset, 0);
- for ($i = 0; ($i < \mb_strlen($string)) && ($wCount < ($words + $offset)); $i++)
- {
- $char = \mb_substr($string, $i, 1);
- if ($char === '<')
- {
- $insideTag = TRUE;
- if (\ctype_alnum($lastChar))
- {
- $wCount++;
- }
- }
- else if ($char === '>')
- {
- $insideTag = FALSE;
- }
- else if (!\ctype_alnum($char))
- {
- if (!$insideTag && \ctype_alnum($lastChar))
- {
- $wCount++;
- }
- }
- $lastChar = $char;
- if (!$insideTag && ($char !== '>') && ($wCount >= $offset))
- {
- $ret .= $char;
- }
- }
- $ret = trim($ret);
- while (!\ctype_alnum(\mb_substr($ret, 0, 1)))
- {
- $ret = \mb_substr($ret, 1);
- }
- return $ret;
- }
Add Comment
Please, Sign In to add comment