adding test and fix for hightlighting tags, old bug 2111 for 1.3 but its the same issue. This should make more options possible

This commit is contained in:
dogmatic69 2012-03-16 21:20:00 +00:00
parent 47a2c22af4
commit d3a44811ce
2 changed files with 10 additions and 3 deletions

View file

@ -455,6 +455,11 @@ podeís adquirirla.</span></p>
$expected = '<b>This</b> is a test <b>text</b>'; $expected = '<b>This</b> is a test <b>text</b>';
$this->assertEquals($expected, $result); $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'; $text = 'This is a test text';
$phrases = null; $phrases = null;
$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>')); $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));

View file

@ -361,6 +361,7 @@ class String {
* *
* - `format` The piece of html with that the phrase will be highlighted * - `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 * - `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 $text Text to search the phrase in
* @param string $phrase The phrase that will be searched * @param string $phrase The phrase that will be searched
@ -375,7 +376,8 @@ class String {
$default = array( $default = array(
'format' => '<span class="highlight">\1</span>', 'format' => '<span class="highlight">\1</span>',
'html' => false 'html' => false,
'regex' => "|%s|iu"
); );
$options = array_merge($default, $options); $options = array_merge($default, $options);
extract($options); extract($options);
@ -391,7 +393,7 @@ class String {
} }
$with[] = (is_array($format)) ? $format[$key] : $format; $with[] = (is_array($format)) ? $format[$key] : $format;
$replace[] = "|$segment|iu"; $replace[] = sprintf($options['regex'], $segment);
} }
return preg_replace($replace, $with, $text); return preg_replace($replace, $with, $text);
@ -401,7 +403,7 @@ class String {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)"; $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
} }
return preg_replace("|$phrase|iu", $format, $text); return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
} }
} }