From d3a44811ce70e6d9777d193e6ae5e001c7146237 Mon Sep 17 00:00:00 2001
From: dogmatic69
Date: Fri, 16 Mar 2012 21:20:00 +0000
Subject: [PATCH] adding test and fix for hightlighting tags, old bug 2111 for
1.3 but its the same issue. This should make more options possible
---
lib/Cake/Test/Case/Utility/StringTest.php | 5 +++++
lib/Cake/Utility/String.php | 8 +++++---
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/lib/Cake/Test/Case/Utility/StringTest.php b/lib/Cake/Test/Case/Utility/StringTest.php
index 2c8f40ea4..80ec96a49 100644
--- a/lib/Cake/Test/Case/Utility/StringTest.php
+++ b/lib/Cake/Test/Case/Utility/StringTest.php
@@ -454,6 +454,11 @@ podeĆs adquirirla.
$result = $this->Text->highlight($text, $phrases, array('format' => '\1'));
$expected = 'This is a test text';
$this->assertEquals($expected, $result);
+
+ $phrases = array('is', 'text');
+ $result = $this->Text->highlight($text, $phrases, array('format' => '\1', 'regex' => "|\b%s\b|iu"));
+ $expected = 'This is a test text';
+ $this->assertEquals($expected, $result);
$text = 'This is a test text';
$phrases = null;
diff --git a/lib/Cake/Utility/String.php b/lib/Cake/Utility/String.php
index ff06d0525..410718455 100644
--- a/lib/Cake/Utility/String.php
+++ b/lib/Cake/Utility/String.php
@@ -361,6 +361,7 @@ class String {
*
* - `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 @@ class String {
$default = array(
'format' => '\1',
- 'html' => false
+ 'html' => false,
+ 'regex' => "|%s|iu"
);
$options = array_merge($default, $options);
extract($options);
@@ -391,7 +393,7 @@ class String {
}
$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 @@ class String {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
- return preg_replace("|$phrase|iu", $format, $text);
+ return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
}
}