Skip to content
Permalink
Browse files

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

…lper::highlight(). Implements #17.
  • Loading branch information...
jperras committed Aug 13, 2009
1 parent 2893b29 commit deed69ea37ad9e3b8a17a981e7ddd845ee6b9d0d
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);
}
}
@@ -164,6 +164,21 @@ function testHighlightConsiderHtml() {
$this->assertEqual($this->Text->highlight($text4, array('strong', 'what'), '<b>\1</b>', true), $expected);
}
/**
* 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
*

0 comments on commit deed69e

Please sign in to comment.
You can’t perform that action at this time.