Permalink
Browse files

Adding ability to input an array parameter for 3rd argument of TextHe…

…lper::highlight(). Implements #17.
  • Loading branch information...
1 parent 2893b29 commit deed69ea37ad9e3b8a17a981e7ddd845ee6b9d0d @jperras jperras committed Aug 13, 2009
Showing with 27 additions and 10 deletions.
  1. +12 −10 cake/libs/view/helpers/text.php
  2. +15 −0 cake/tests/cases/libs/view/helpers/text.test.php
@@ -64,28 +64,30 @@ function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</s
}
if (is_array($phrase)) {
+
$replace = array();
$with = array();
- foreach ($phrase as $key => $value) {
- $key = $value;
- $value = $highlighter;
- $key = '(' . $key . ')';
+ foreach ($phrase as $key => $segment) {
+ $segment = "($segment)";
+
if ($considerHtml) {
- $key = '(?![^<]+>)' . $key . '(?![^<]+>)';
+ $segment = "(?![^<]+>)$segment(?![^<]+>)";
}
- $replace[] = '|' . $key . '|iu';
- $with[] = empty($value) ? $highlighter : $value;
+
+ $with[] = (is_array($highlighter)) ? $highlighter[$key] : $highlighter;
+ $replace[] = "|$segment|iu";
}
return preg_replace($replace, $with, $text);
+
} else {
- $phrase = '(' . $phrase . ')';
+ $phrase = "($phrase)";
if ($considerHtml) {
- $phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)';
+ $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
- return preg_replace('|'.$phrase.'|iu', $highlighter, $text);
+ return preg_replace("|$phrase|iu", $highlighter, $text);
}
}
@@ -165,6 +165,21 @@ function testHighlightConsiderHtml() {
}
/**
+ * testHighlightMulti method
+ *
+ * @access public
+ * @return void
+ */
+ function testHighlightMulti() {
+ $text = 'This is a test text';
+ $phrases = array('This', 'text');
+ $result = $this->Text->highlight($text, $phrases, array('<b>\1</b>', '<em>\1</em>'));
+ $expected = '<b>This</b> is a test <em>text</em>';
+ $this->assertEqual($expected, $result);
+
+ }
+
+/**
* testStripLinks method
*
* @access public

0 comments on commit deed69e

Please sign in to comment.