Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding test and fix for hightlighting tags, old bug 2111 for 1.3 but …

…its the same issue. This should make more options possible
  • Loading branch information...
commit d3a44811ce70e6d9777d193e6ae5e001c7146237 1 parent 47a2c22
@dogmatic69 dogmatic69 authored
View
5 lib/Cake/Test/Case/Utility/StringTest.php
@@ -454,6 +454,11 @@ public function testHighlight() {
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
$expected = '<b>This</b> is a test <b>text</b>';
$this->assertEquals($expected, $result);
+
+ $phrases = array('is', 'text');
+ $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>', 'regex' => "|\b%s\b|iu"));
+ $expected = 'This <b>is</b> a test <b>text</b>';
+ $this->assertEquals($expected, $result);
$text = 'This is a test text';
$phrases = null;
View
8 lib/Cake/Utility/String.php
@@ -361,6 +361,7 @@ public static function wrap($text, $options = array()) {
*
* - `format` The piece of html with that the phrase will be highlighted
* - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
+ * - `regex` a custom regex rule that is ued to match words, default is '|$tag|iu'
*
* @param string $text Text to search the phrase in
* @param string $phrase The phrase that will be searched
@@ -375,7 +376,8 @@ public static function highlight($text, $phrase, $options = array()) {
$default = array(
'format' => '<span class="highlight">\1</span>',
- 'html' => false
+ 'html' => false,
+ 'regex' => "|%s|iu"
);
$options = array_merge($default, $options);
extract($options);
@@ -391,7 +393,7 @@ public static function highlight($text, $phrase, $options = array()) {
}
$with[] = (is_array($format)) ? $format[$key] : $format;
- $replace[] = "|$segment|iu";
+ $replace[] = sprintf($options['regex'], $segment);
}
return preg_replace($replace, $with, $text);
@@ -401,7 +403,7 @@ public static function highlight($text, $phrase, $options = array()) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
- return preg_replace("|$phrase|iu", $format, $text);
+ return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.