Skip to content
This repository
Browse code

Fix TextHelper excerpt method to work as documented

Fixes #2339
  • Loading branch information...
commit d3e8f9f803bf614fab8aad1e7923d60115ddb25f 1 parent ca098eb
Kyle Robinson Young authored December 05, 2011
29  cake/libs/view/helpers/text.php
@@ -287,34 +287,31 @@ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
287 287
 			return $this->truncate($text, $radius * 2, array('ending' => $ending));
288 288
 		}
289 289
 
  290
+		$append = $prepend = $ending;
  291
+
290 292
 		$phraseLen = mb_strlen($phrase);
291  
-		if ($radius < $phraseLen) {
292  
-			$radius = $phraseLen;
293  
-		}
  293
+		$textLen = mb_strlen($text);
294 294
 
295 295
 		$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
296  
-
297  
-		$startPos = 0;
298  
-		if ($pos > $radius) {
299  
-			$startPos = $pos - $radius;
  296
+		if ($pos === false) {
  297
+			return mb_substr($text, 0, $radius) . $ending;
300 298
 		}
301 299
 
302  
-		$textLen = mb_strlen($text);
  300
+		$startPos = $pos - $radius;
  301
+		if ($startPos <= 0) {
  302
+			$startPos = 0;
  303
+			$prepend = '';
  304
+		}
303 305
 
304 306
 		$endPos = $pos + $phraseLen + $radius;
305 307
 		if ($endPos >= $textLen) {
306 308
 			$endPos = $textLen;
  309
+			$append = '';
307 310
 		}
308 311
 
309 312
 		$excerpt = mb_substr($text, $startPos, $endPos - $startPos);
310  
-		if ($startPos != 0) {
311  
-			$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
312  
-		}
313  
-
314  
-		if ($endPos != $textLen) {
315  
-			$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
316  
-		}
317  
-
  313
+		$excerpt = $prepend . $excerpt . $append;
  314
+		
318 315
 		return $excerpt;
319 316
 	}
320 317
 
19  cake/tests/cases/libs/view/helpers/text.test.php
@@ -362,7 +362,7 @@ function testHighlightCaseInsensitivity() {
362 362
 	function testExcerpt() {
363 363
 		$text = 'This is a phrase with test text to play with';
364 364
 
365  
-		$expected = '...with test text...';
  365
+		$expected = '...ase with test text to ...';
366 366
 		$result = $this->Text->excerpt($text, 'test', 9, '...');
367 367
 		$this->assertEqual($expected, $result);
368 368
 
@@ -378,18 +378,19 @@ function testExcerpt() {
378 378
 		$result = $this->Text->excerpt($text, null, 200, '...');
379 379
 		$this->assertEqual($expected, $result);
380 380
 
381  
-		$expected = '...phrase...';
  381
+		$expected = '...a phrase w...';
382 382
 		$result = $this->Text->excerpt($text, 'phrase', 2, '...');
383 383
 		$this->assertEqual($expected, $result);
384 384
 
385  
-		$phrase = 'This is a phrase with test';
  385
+		$phrase = 'This is a phrase with test text';
386 386
 		$expected = $text;
387  
-		$result = $this->Text->excerpt($text, $phrase, strlen($phrase) + 3, '...');
  387
+		$result = $this->Text->excerpt($text, $phrase, 13, '...');
388 388
 		$this->assertEqual($expected, $result);
389  
-
390  
-		$phrase = 'This is a phrase with text';
391  
-		$expected = $text;
392  
-		$result = $this->Text->excerpt($text, $phrase, 10, '...');
  389
+		
  390
+		$text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
  391
+		$phrase = 'bbbbbbbb';
  392
+		$result = $this->Text->excerpt($text, $phrase, 10);
  393
+		$expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
393 394
 		$this->assertEqual($expected, $result);
394 395
 	}
395 396
 
@@ -402,7 +403,7 @@ function testExcerpt() {
402 403
 	function testExcerptCaseInsensitivity() {
403 404
 		$text = 'This is a phrase with test text to play with';
404 405
 
405  
-		$expected = '...with test text...';
  406
+		$expected = '...ase with test text to ...';
406 407
 		$result = $this->Text->excerpt($text, 'TEST', 9, '...');
407 408
 		$this->assertEqual($expected, $result);
408 409
 

0 notes on commit d3e8f9f

Please sign in to comment.
Something went wrong with that request. Please try again.