wraps the provided keywords in a text with span tags containing the highlight css tag.

@param string $text
@param array  $words

@return string of processed text
491384
public function highlight($text, $words, $min = 3)
    {
        $words = AnConfig::unbox($words);
        settype($words, 'array');
        foreach ($words as $word) {
            if (strlen($word) >= $min) {
                $text = AnHelperString::str_ireplace($word, '<span class="an-text-highlight">'.$word.'</span>', $text);
            }
        }

        return $text;
    }
968828
public function render(string $text)
    {
        $text = explode("\n", trim($text));
        $min  = PHP_INT_MAX;
        $max  = 0;
        foreach ($text as $line) {
            $len = strlen(strip_tags($line, $this->options[self::ALLOWED_TAGS]));
            if ($len > $max) {
                $max = $len;
            }
            if ($len < $min) {
                $min = $len;
            }
        }

        // excluding border
        $cols = $max + $this->options[self::PADDING_LEFT_RIGHT] * 2;

        $lines = [];

        // top margin
        for ($i = 0; $i < $this->options[self::MARGIN_TOP]; $i++) {
            $lines[] = '';
        }

        // top
        $lines[] = $this->options[self::TOP_LEFT]
            . implode('', array_fill(0, $cols, $this->options[self::HORIZONTAL_LINE]))
            . $this->options[self::TOP_RIGHT];

        // padding top
        for ($i = 0; $i < $this->options[self::PADDING_TOP_BOTTOM]; $i++) {
            $lines[] = $this->options[self::VERTICAL_LINE]
                . implode('', array_fill(0, $cols, ' '))
                . $this->options[self::VERTICAL_LINE];
        }

        // text
        for ($i = 0; $i < count($text); $i++) {
            $length = strlen(strip_tags($text[$i], $this->options[self::ALLOWED_TAGS]));
            $left   = $this->options[self::CENTERED]
                ? $this->options[self::PADDING_LEFT_RIGHT] + (int)floor(($max - $length) / 2)
                : $this->options[self::PADDING_LEFT_RIGHT];
            $right  = $this->options[self::CENTERED]
                ? $this->options[self::PADDING_LEFT_RIGHT] + (int)ceil(($max - $length) / 2)
                : $cols - $length - $this->options[self::PADDING_LEFT_RIGHT];
            $lines[] = $this->options[self::VERTICAL_LINE]
                . implode('', array_fill(0, $left, ' '))
                . $text[$i]
                . implode('', array_fill(0, $right, ' '))
                . $this->options[self::VERTICAL_LINE];
        }

        // padding bottom
        for ($i = 0; $i < $this->options[self::PADDING_TOP_BOTTOM]; $i++) {
            $lines[] = $this->options[self::VERTICAL_LINE]
                . implode('', array_fill(0, $cols, ' ')) . $this->options[self::VERTICAL_LINE];
        }

        // bottom
        $lines[] = $this->options[self::BOTTOM_LEFT]
            . implode('', array_fill(0, $cols, $this->options[self::HORIZONTAL_LINE]))
            . $this->options[self::BOTTOM_RIGHT];

        // bottom margin
        for ($i = 0; $i < $this->options[self::MARGIN_BOTTOM]; $i++) {
            $lines[] = '';
        }

        $this->stream->write(implode("\n", $lines));
    }
998213
public function format(string $text) : string
    {
        if ($text === '') {
            return $text;
        }

        $startCodes = [];
        $endCodes = [];

        if ($this->foregroundColor !== null) {
            $startCodes[] = self::$supportedForegroundColors[$this->foregroundColor][0];
            $endCodes[] = self::$supportedForegroundColors[$this->foregroundColor][1];
        }

        if ($this->backgroundColor !== null) {
            $startCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][0];
            $endCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][1];
        }

        foreach ($this->textStyles as $style) {
            $startCodes[] = self::$supportedTextStyles[$style][0];
            $endCodes[] = self::$supportedTextStyles[$style][1];
        }

        if (count($startCodes) === 0 && count($endCodes) === 0) {
            // No point in trying to format the text
            return $text;
        }

        return sprintf(
            "\033[%sm%s\033[%sm",
            implode(';', $startCodes),
            $text,
            implode(';', $endCodes)
        );
    }
616269
public function addText($text) {
		$content = new HtmlString();
		$content->htmlString = $text;
		$this->children[] = $content;
		return $this;
	}
957508
private function _convertTagsToEscapeSequences ( $text ) {
        list($patterns, $replacements) = $this->_getConvertingDictionary();
        $convertedText = preg_replace($patterns, $replacements, $text);
        return $convertedText;
    }
393865
protected function renderText($text)
	{
		if ($this->enableNewlines) {
			return preg_replace("/(  \n|\n)/", "\\\\\\\\\n", $this->escapeLatex($text[1]));
		} else {
			return parent::renderText($text);
		}
	}
1049021
protected function _parseTags(&$text)
    {
        $tags = '';

        $matches = array();
        if(preg_match_all('#<ktml:style\s+src="([^"]+)"(.*)\/>#siU', $text, $matches))
        {
            foreach(array_unique($matches[1]) as $key => $match)
            {
                //Set required attributes
                $attribs = array(
                    'src' => $match
                );

                $attribs = array_merge($this->parseAttributes( $matches[2][$key]), $attribs);
                $tags .= $this->_renderTag($attribs);
            }

            $text = str_replace($matches[0], '', $text);
        }

        $matches = array();
        if(preg_match_all('#<style(.*)>(.*)<\/style>#siU', $text, $matches))
        {
            foreach($matches[2] as $key => $match)
            {
                $attribs = $this->parseAttributes( $matches[1][$key]);
                $tags .= $this->_renderTag($attribs, $match);
            }

            $text = str_replace($matches[0], '', $text);
        }

        return $tags;
    }
652825
public function highlight($text, $needle, $tag = 'em', $options = [])
    {
        $this->options = array_merge($this->options, $options);

        $tagAttributes = '';
        if (count($this->options['tagOptions'])) {
            foreach ($this->options['tagOptions'] as $attr => $value) {
                $tagAttributes .= $attr . '="' . $value . '" ';
            }
            $tagAttributes = ' ' . trim($tagAttributes);
        }

        $highlight = '<' . $tag . $tagAttributes .'>\1</' . $tag . '>';
        $needle    = preg_split('/\PL+/u', $needle, -1, PREG_SPLIT_NO_EMPTY);

        // Select pattern to use
        if ($this->options['simple']) {
            $pattern    = '#(%s)#';
            $sl_pattern = '#(%s)#';
        } else {
            $pattern    = '#(?!<.*?)(%s)(?![^<>]*?>)#';
            $sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
        }

	    // Add Forgotten Unicode
	    $pattern .= 'u';

        // Case sensitivity
        if (!($this->options['caseSensitive'])) {
            $pattern .= 'i';
            $sl_pattern .= 'i';
        }

        $needle = (array) $needle;
        foreach ($needle as $needle_s) {
            $needle_s = preg_quote($needle_s);

            // Escape needle with optional whole word check
            if ($this->options['wholeWord']) {
                $needle_s = '\b' . $needle_s . '\b';
            }

            // Strip links
            if ($this->options['stripLinks']) {
                $sl_regex = sprintf($sl_pattern, $needle_s);
                $text     = preg_replace($sl_regex, '\1', $text);
            }

            $regex = sprintf($pattern, $needle_s);
            $text  = preg_replace($regex, $highlight, $text);
        }

        return $text;
    }
323755
public function highlightSpecialWords($string)
    {
        // read specialword-table if not already read-in
        if ($this->_specialWordTable === null) {
            $this->_createSpecialWordTable();
        }

        // iterate over possible existing tags
        foreach ($this->_tags as $tag) {
            // get words for tags
            $words = $this->_specialWordTable[$tag];

            // iterate over words as word => description
            foreach ($words as $word => $description) {
                // replace it with template content if word found
                if (mb_strstr($string, $word)) {
                    // get template for tag
                    $tpl = $this->_templates[$tag];

                    // create HTML code
                    $html = str_replace('{$WORD}', $word, str_replace('{$DESC}', $description, $tpl));

                    // insert HTML into given string
                    $string = str_replace($word, $html, $string);
                }
            }
        }

        // return processed string result
        return $string;
    }
581700
protected function formatText($text) {
        $syntax = [
            '/\<strong\>/m' => "\033[1m",
            '/\<u\>/m' => "\033[4m",
            '/\<dim\>/m' => "\033[2m",
            '/\<fatal\>/m' => "\033[" . $this->colors[self::FATAL] . "m",
            '/\<error\>/m' => "\033[" . $this->colors[self::ERROR] . "m",
            '/\<warning\>/m' => "\033[" . $this->colors[self::WARNING] . "m",
            '/\<notice\>/m' => "\033[" . $this->colors[self::NOTICE] . "m",
            '/\<debug\>/m' => "\033[" . $this->colors[self::DEBUG] . "m",
            '/\<red\>/m' => "\033[31m",
            '/\<yellow\>/m' => "\033[33m",
            '/\<green\>/m' => "\033[32m",
            '/\<grey\>/m' => "\033[37m",
            '/\<\/([a-z]+)\>/m' => "\033[0m",
        ];

        $replacements = '$1';

        if ($this->colorize) {
            $replacements = $syntax;
        }

        $result = preg_replace(
            array_keys($syntax),
            $replacements,
            $text
        );

        if (!is_string($result)) {
            throw new RuntimeException('Unable to format text', ExitApp::RUNTIME_ERROR);
        }

        return $result;
    }
368428
public function processText($text)
    {
        if ((string) $text != '') {
            $store = array();
            $_store = 0;
            $_offset = 0;
            if ($this->parser->strip) {
                if (strpos($text, '<') !== false) {
                    // capture html elements not to be messed with
                    $_offset = 0;
                    if (preg_match_all('#(<script[^>]*>.*?</script[^>]*>)|(<textarea[^>]*>.*?</textarea[^>]*>)|(<pre[^>]*>.*?</pre[^>]*>)#is',
                                       $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
                        foreach ($matches as $match) {
                            $store[] = $match[ 0 ][ 0 ];
                            $_length = strlen($match[ 0 ][ 0 ]);
                            $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
                            $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] - $_offset, $_length);

                            $_offset += $_length - strlen($replace);
                            $_store ++;
                        }
                    }

                    $expressions = array(// replace multiple spaces between tags by a single space
                                         // can't remove them entirely, becaue that might break poorly implemented CSS display:inline-block elements
                                         '#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s'                                    => '\1 \2',
                                         // remove spaces between attributes (but not in attribute values!)
                                         '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5',
                                         '#^\s+<#Ss'                                                               => '<',
                                         '#>\s+$#Ss'                                                               => '>',
                                         $this->stripRegEx                                                         => '');

                    $text = preg_replace(array_keys($expressions), array_values($expressions), $text);
                    $_offset = 0;
                    if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $text, $matches,
                                       PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
                        foreach ($matches as $match) {
                            $_length = strlen($match[ 0 ][ 0 ]);
                            $replace = $store[ $match[ 1 ][ 0 ] ];
                            $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] + $_offset, $_length);

                            $_offset += strlen($replace) - $_length;
                            $_store ++;
                        }
                    }
                } else {
                    $text = preg_replace($this->stripRegEx, '', $text);
                }
            }
            return new Smarty_Internal_ParseTree_Text($text);
        }
        return null;
    }
132545
public static function match($keyword, $text = '', &$matches = false) {
        return (bool)preg_match(sprintf(self::$re, self::regex($keyword)), $text, $matches, PREG_OFFSET_CAPTURE);
    }
488820
private function parseLinks($text)
    {
        $offset = 0;
        $matches = array();
        // Extract all links from the text of the passage (if they exist)
        while (preg_match($this->links_pattern, $text, $matches, 0, $offset)) {
            switch ($matches[2]) {
                case '->':
                    $this->links[] = array('text' => trim($matches[1]), 'link' => trim($matches[3]));
                    $this->raw_links[] = $matches[0];
                    break;
                case '<-':
                case '|':
                    $this->links[] = array('text' => trim($matches[3]), 'link' => trim($matches[1]));
                    $this->raw_links[] = $matches[0];
                    break;
                default:
                    throw new \RuntimeException("Something that looked like a link could not be parsed.");
            }
            $offset = mb_stripos($text, $matches[0]) + mb_strlen($matches[0]);
        }
    }
100713
protected function _parseStyles(&$text)
    {
        $styles = '';

        $matches = array();
        if (preg_match_all('#<style\s*src="([^"]+)"(.*)\/>#iU', $text, $matches)) {
            foreach (array_unique($matches[1]) as $key => $match) {
                $attribs = $this->_parseAttributes($matches[2][$key]);
                $styles .= $this->_renderStyle($match, true, $attribs);
            }

            $text = str_replace($matches[0], '', $text);
        }

        $matches = array();
        if (preg_match_all('#<style(.*)>(.*)<\/style>#siU', $text, $matches)) {
            foreach ($matches[2] as $key => $match) {
                $attribs = $this->_parseAttributes($matches[1][$key]);
                $styles .= $this->_renderStyle($match, false, $attribs);
            }

            $text = str_replace($matches[0], '', $text);
        }

        return $styles;
    }
1055876
public static function em($text, $htmlOptions = array(), $tag = 'p')
    {
        $color = TbArray::popValue('color', $htmlOptions);
        if (TbArray::popValue('muted', $htmlOptions, false)) {
            self::addCssClass('muted', $htmlOptions);
        } else {
            if (!empty($color)) {
                self::addCssClass('text-' . $color, $htmlOptions);
            }
        }
        return self::tag($tag, $htmlOptions, $text);
    }
239816
public function highlight($text, array $options = []) {
		$options += $this->_config;

		$text = $this->_prepare($text);

		if ($options['escape'] !== false) {
			$text = h($text);
		}

		$attr = ['class' => $options['prefix'] . $options['lang']];

		$options['attr'] = $this->templater()->formatAttributes($attr);
		$options['content'] = $text;

		return $this->templater()->format('code', $options);
	}
1327052
private function parseBlock($text, &$lines)
    {
        $lines = explode("\n", $text);
        $this->_blocks = array();
        $this->_current = 'normal';
        $this->_pos = -1;
        $state = array(
            'special'   =>  implode("|", array_keys($this->_specialWhiteList)),
            'empty'     =>  0,
            'html'      =>  false
        );
        // analyze by line
        foreach ($lines as $key => $line) {
            $block = $this->getBlock();
            $args = array($block, $key, $line, &$state, $lines);
            if ($this->_current != 'normal') {
                $pass = call_user_func_array($this->_parsers[$this->_current], $args);
                if (!$pass) {
                    continue;
                }
            }
            foreach ($this->_parsers as $name => $parser) {
                if ($name != $this->_current) {
                    $pass = call_user_func_array($parser, $args);
                    if (!$pass) {
                        break;
                    }
                }
            }
        }
        return $this->optimizeBlocks($this->_blocks, $lines);
    }