Skip to content
This repository
Browse code

String::tail()

  • Loading branch information...
commit 917d912a43d1b7d22b6b86a60d8904cbc70490a7 1 parent 22c1ac9
Mark authored August 02, 2012
40  lib/Cake/Test/Case/Utility/StringTest.php
@@ -444,6 +444,46 @@ public function testTruncate() {
444 444
 	}
445 445
 
446 446
 /**
  447
+ * testTail method
  448
+ *
  449
+ * @return void
  450
+ */
  451
+	public function testTail() {
  452
+		$text1 = 'The quick brown fox jumps over the lazy dog';
  453
+		$text2 = 'Heizölrückstoßabdämpfung';
  454
+		$text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
  455
+		$text4 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
  456
+		$text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
  457
+
  458
+		$result = $this->Text->tail($text1, 13);
  459
+		$this->assertEquals('...e lazy dog', $result);
  460
+
  461
+		$result = $this->Text->tail($text1, 13, array('exact' => false));
  462
+		$this->assertEquals('...lazy dog', $result);
  463
+
  464
+		$result = $this->Text->tail($text1, 100);
  465
+		$this->assertEquals('The quick brown fox jumps over the lazy dog', $result);
  466
+
  467
+		$result = $this->Text->tail($text2, 10);
  468
+		$this->assertEquals('...;mpfung', $result);
  469
+
  470
+		$result = $this->Text->tail($text2, 10, array('exact' => false));
  471
+		$this->assertEquals('...', $result);
  472
+
  473
+		$result = $this->Text->tail($text3, 255);
  474
+		$this->assertEquals($text3, $result);
  475
+
  476
+		$result = $this->Text->tail($text3, 21);
  477
+		$this->assertEquals('...á dicho la verdad?', $result);
  478
+
  479
+		$result = $this->Text->tail($text4, 25);
  480
+		$this->assertEquals('...a R' . chr(195) . chr(169) . 'publique de France', $result);
  481
+
  482
+		$result = $this->Text->tail($text5, 10);
  483
+		$this->assertEquals('...цчшщъыь', $result);
  484
+	}
  485
+
  486
+/**
447 487
  * testHighlight method
448 488
  *
449 489
  * @return void
40  lib/Cake/Utility/String.php
@@ -419,6 +419,46 @@ public static function stripLinks($text) {
419 419
 	}
420 420
 
421 421
 /**
  422
+ * Truncates text starting from the end.
  423
+ *
  424
+ * Cuts a string to the length of $length and replaces the first characters
  425
+ * with the beginning if the text is longer than length.
  426
+ *
  427
+ * ### Options:
  428
+ *
  429
+ * - `beginning` Will be used as Beginning and prepended to the trimmed string
  430
+ * - `exact` If false, $text will not be cut mid-word
  431
+ *
  432
+ * @param string $text String to truncate.
  433
+ * @param integer $length Length of returned string, including ellipsis.
  434
+ * @param array $options An array of html attributes and options.
  435
+ * @return string Trimmed string.
  436
+ */
  437
+	public static function tail($text, $length = 100, $options = array()) {
  438
+		$default = array(
  439
+			'beginning' => '...', 'exact' => true
  440
+		);
  441
+		$options = array_merge($default, $options);
  442
+		extract($options);
  443
+
  444
+		if (!function_exists('mb_strlen')) {
  445
+			class_exists('Multibyte');
  446
+		}
  447
+
  448
+		if (mb_strlen($text) <= $length) {
  449
+			return $text;
  450
+		} else {
  451
+			$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($beginning));
  452
+		}
  453
+		if (!$exact) {
  454
+			$spacepos = mb_strpos($truncate, ' ');
  455
+			$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
  456
+		}
  457
+
  458
+		return $beginning . $truncate;
  459
+	}
  460
+
  461
+/**
422 462
  * Truncates text.
423 463
  *
424 464
  * Cuts a string to the length of $length and replaces the last characters

0 notes on commit 917d912

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