diff --git a/cake/libs/view/helpers/text.php b/cake/libs/view/helpers/text.php index d22214c10f6..4d8d1e0b55c 100644 --- a/cake/libs/view/helpers/text.php +++ b/cake/libs/view/helpers/text.php @@ -65,21 +65,19 @@ function highlight($text, $phrase, $highlighter = '\1 $value) { $key = $value; $value = $highlighter; - $key = '(' . $key . ')'; + $key = '(' . preg_quote($key, '|') . ')'; if ($considerHtml) { $key = '(?![^<]+>)' . $key . '(?![^<]+>)'; } $replace[] = '|' . $key . '|iu'; $with[] = empty($value) ? $highlighter : $value; } - return preg_replace($replace, $with, $text); } else { - $phrase = '(' . $phrase . ')'; + $phrase = '(' . preg_quote($phrase, '|') . ')'; if ($considerHtml) { $phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)'; } - return preg_replace('|'.$phrase.'|iu', $highlighter, $text); } } diff --git a/cake/tests/cases/libs/view/helpers/text.test.php b/cake/tests/cases/libs/view/helpers/text.test.php index 61e58b43cf1..cb70e1ad081 100644 --- a/cake/tests/cases/libs/view/helpers/text.test.php +++ b/cake/tests/cases/libs/view/helpers/text.test.php @@ -125,6 +125,11 @@ function testHighlight() { $result = $this->Text->highlight($text, $phrases, '\1'); $this->assertEqual($result, $text); + $text = 'This is a (test) text'; + $phrases = '(test'; + $result = $this->Text->highlight($text, $phrases, '\1'); + $this->assertEqual('This is a (test) text', $result); + $text = 'Ich saß in einem Café am Übergang'; $expected = 'Ich saß in einem Café am Übergang'; $phrases = array('saß', 'café', 'übergang');