Skip to content

Commit

Permalink
Adding ability to input an array parameter for 3rd argument of TextHe…
Browse files Browse the repository at this point in the history
…lper::highlight(). Implements #17.
  • Loading branch information
jperras committed Aug 13, 2009
1 parent 2893b29 commit deed69e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
22 changes: 12 additions & 10 deletions cake/libs/view/helpers/text.php
Expand Up @@ -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);
}
}

Expand Down
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/view/helpers/text.test.php
Expand Up @@ -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
*
Expand Down

0 comments on commit deed69e

Please sign in to comment.