Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

String::tail()

  • Loading branch information...
commit 917d912a43d1b7d22b6b86a60d8904cbc70490a7 1 parent 22c1ac9
@dereuromark dereuromark authored
View
40 lib/Cake/Test/Case/Utility/StringTest.php
@@ -444,6 +444,46 @@ public function testTruncate() {
}
/**
+ * testTail method
+ *
+ * @return void
+ */
+ public function testTail() {
+ $text1 = 'The quick brown fox jumps over the lazy dog';
+ $text2 = 'Heizölrückstoßabdämpfung';
+ $text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
+ $text4 = 'Vive la R' . chr(195) . chr(169) . 'publique de France';
+ $text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
+
+ $result = $this->Text->tail($text1, 13);
+ $this->assertEquals('...e lazy dog', $result);
+
+ $result = $this->Text->tail($text1, 13, array('exact' => false));
+ $this->assertEquals('...lazy dog', $result);
+
+ $result = $this->Text->tail($text1, 100);
+ $this->assertEquals('The quick brown fox jumps over the lazy dog', $result);
+
+ $result = $this->Text->tail($text2, 10);
+ $this->assertEquals('...;mpfung', $result);
+
+ $result = $this->Text->tail($text2, 10, array('exact' => false));
+ $this->assertEquals('...', $result);
+
+ $result = $this->Text->tail($text3, 255);
+ $this->assertEquals($text3, $result);
+
+ $result = $this->Text->tail($text3, 21);
+ $this->assertEquals('...á dicho la verdad?', $result);
+
+ $result = $this->Text->tail($text4, 25);
+ $this->assertEquals('...a R' . chr(195) . chr(169) . 'publique de France', $result);
+
+ $result = $this->Text->tail($text5, 10);
+ $this->assertEquals('...цчшщъыь', $result);
+ }
+
+/**
* testHighlight method
*
* @return void
View
40 lib/Cake/Utility/String.php
@@ -419,6 +419,46 @@ public static function stripLinks($text) {
}
/**
+ * Truncates text starting from the end.
+ *
+ * Cuts a string to the length of $length and replaces the first characters
+ * with the beginning if the text is longer than length.
+ *
+ * ### Options:
+ *
+ * - `beginning` Will be used as Beginning and prepended to the trimmed string
+ * - `exact` If false, $text will not be cut mid-word
+ *
+ * @param string $text String to truncate.
+ * @param integer $length Length of returned string, including ellipsis.
+ * @param array $options An array of html attributes and options.
+ * @return string Trimmed string.
+ */
+ public static function tail($text, $length = 100, $options = array()) {
+ $default = array(
+ 'beginning' => '...', 'exact' => true
+ );
+ $options = array_merge($default, $options);
+ extract($options);
+
+ if (!function_exists('mb_strlen')) {
+ class_exists('Multibyte');
+ }
+
+ if (mb_strlen($text) <= $length) {
+ return $text;
+ } else {
+ $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($beginning));
+ }
+ if (!$exact) {
+ $spacepos = mb_strpos($truncate, ' ');
+ $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
+ }
+
+ return $beginning . $truncate;
+ }
+
+/**
* Truncates text.
*
* Cuts a string to the length of $length and replaces the last characters

0 comments on commit 917d912

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