/** * Apply a prefix and suffix to a set of words in the given text. * Used by the {@link HTML_MUNGER} to provide search term highlighting. * * @param string $text Text to search. * @param string $phrase Space-separated list of words. * @param string $prefix Prepended to each found word. * @param string $suffix Appended to each found word. * @return string */ public static function select_words($text, $phrase, $prefix, $suffix) { if ($phrase) { $preg_phrase = REGULAR_EXPRESSION::sanitize_words($phrase); /* Break into separate words and build a list of regular expressions. */ $words = explode(' ', trim($preg_phrase)); foreach ($words as $word) { $preg_words[] = "/(^|[^A-Za-z0-9])({$word})([^A-Za-z0-9]|\$)/i"; } /* Replace the words, passing in the array so that the regexp engine in PHP is only initialized once. */ return preg_replace($preg_words, "\\1{$prefix}\\2{$suffix}\\3", $text); } return $text; }
protected function _run_sanitize_words_tests() { $this->_num_tests += 1; $this->_check_equal('\\/', REGULAR_EXPRESSION::sanitize_words("/")); $this->_check_equal('\\', REGULAR_EXPRESSION::sanitize_words("\\")); $this->_check_equal('\\|', REGULAR_EXPRESSION::sanitize_words("|")); $this->_check_equal('\\(', REGULAR_EXPRESSION::sanitize_words("(")); $this->_check_equal('\\)', REGULAR_EXPRESSION::sanitize_words(")")); $this->_check_equal('\\[', REGULAR_EXPRESSION::sanitize_words("[")); $this->_check_equal('\\]', REGULAR_EXPRESSION::sanitize_words("]")); $this->_check_equal('\\.', REGULAR_EXPRESSION::sanitize_words(".")); $this->_check_equal('\\*', REGULAR_EXPRESSION::sanitize_words("*")); $this->_check_equal('\\+', REGULAR_EXPRESSION::sanitize_words("+")); $this->_check_equal('\\(\\.\\*\\[a\\|b\\]\\+\\)', REGULAR_EXPRESSION::sanitize_words("(.*[a|b]+)")); }
/** * Convert the text to an output format. * @param HTML_MUNGER $munger The conversion context. * @param string $text * @return string * @access private */ protected function _convert($munger, $text) { if ($munger->highlighted_words) { $text = REGULAR_EXPRESSION::select_words($text, $munger->highlighted_words, $this->highlight_prefix, $this->highlight_suffix); } return $text; }