Skip to content

Commit

Permalink
Fix TextHelper excerpt method to work as documented
Browse files Browse the repository at this point in the history
Fixes #2339
  • Loading branch information
shama committed Dec 5, 2011
1 parent ba81754 commit 7f22fcd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
19 changes: 10 additions & 9 deletions lib/Cake/Test/Case/View/Helper/TextHelperTest.php
Expand Up @@ -354,7 +354,7 @@ public function testHighlightCaseInsensitivity() {
public function testExcerpt() {
$text = 'This is a phrase with test text to play with';

$expected = '...with test text...';
$expected = '...ase with test text to ...';
$result = $this->Text->excerpt($text, 'test', 9, '...');
$this->assertEquals($expected, $result);

Expand All @@ -370,18 +370,19 @@ public function testExcerpt() {
$result = $this->Text->excerpt($text, null, 200, '...');
$this->assertEquals($expected, $result);

$expected = '...phrase...';
$expected = '...a phrase w...';
$result = $this->Text->excerpt($text, 'phrase', 2, '...');
$this->assertEquals($expected, $result);

$phrase = 'This is a phrase with test';
$phrase = 'This is a phrase with test text';
$expected = $text;
$result = $this->Text->excerpt($text, $phrase, strlen($phrase) + 3, '...');
$result = $this->Text->excerpt($text, $phrase, 13, '...');
$this->assertEquals($expected, $result);

$phrase = 'This is a phrase with text';
$expected = $text;
$result = $this->Text->excerpt($text, $phrase, 10, '...');

$text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
$phrase = 'bbbbbbbb';
$result = $this->Text->excerpt($text, $phrase, 10);
$expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
$this->assertEquals($expected, $result);
}

Expand All @@ -393,7 +394,7 @@ public function testExcerpt() {
public function testExcerptCaseInsensitivity() {
$text = 'This is a phrase with test text to play with';

$expected = '...with test text...';
$expected = '...ase with test text to ...';
$result = $this->Text->excerpt($text, 'TEST', 9, '...');
$this->assertEquals($expected, $result);

Expand Down
29 changes: 13 additions & 16 deletions lib/Cake/View/Helper/TextHelper.php
Expand Up @@ -321,34 +321,31 @@ public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
return $this->truncate($text, $radius * 2, array('ending' => $ending));
}

$append = $prepend = $ending;

$phraseLen = mb_strlen($phrase);
if ($radius < $phraseLen) {
$radius = $phraseLen;
}
$textLen = mb_strlen($text);

$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));

$startPos = 0;
if ($pos > $radius) {
$startPos = $pos - $radius;
if ($pos === false) {
return mb_substr($text, 0, $radius) . $ending;
}

$textLen = mb_strlen($text);
$startPos = $pos - $radius;
if ($startPos <= 0) {
$startPos = 0;
$prepend = '';
}

$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$endPos = $textLen;
$append = '';
}

$excerpt = mb_substr($text, $startPos, $endPos - $startPos);
if ($startPos != 0) {
$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
}

if ($endPos != $textLen) {
$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
}

$excerpt = $prepend . $excerpt . $append;

return $excerpt;
}

Expand Down

0 comments on commit 7f22fcd

Please sign in to comment.