Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getWords(string $string, int $words): string
- {
- $ret = '';
- $insideTag = FALSE;
- $wCount = 0;
- $lastChar = NULL;
- for ($i = 0; ($i < \mb_strlen($string)) && ($wCount < $words); $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 !== '>'))
- {
- $ret .= $char;
- }
- }
- $ret = trim($ret);
- return $ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement