From 917d912a43d1b7d22b6b86a60d8904cbc70490a7 Mon Sep 17 00:00:00 2001
From: euromark
Date: Thu, 2 Aug 2012 13:21:31 +0200
Subject: [PATCH] String::tail()
---
lib/Cake/Test/Case/Utility/StringTest.php | 40 +++++++++++++++++++++++
lib/Cake/Utility/String.php | 40 +++++++++++++++++++++++
2 files changed, 80 insertions(+)
diff --git a/lib/Cake/Test/Case/Utility/StringTest.php b/lib/Cake/Test/Case/Utility/StringTest.php
index efa024b5b..9b6fe1e2b 100644
--- a/lib/Cake/Test/Case/Utility/StringTest.php
+++ b/lib/Cake/Test/Case/Utility/StringTest.php
@@ -443,6 +443,46 @@ podeís adquirirla.
$this->assertEquals($expected, $result);
}
+/**
+ * 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
*
diff --git a/lib/Cake/Utility/String.php b/lib/Cake/Utility/String.php
index 01f9acbbb..4330ea258 100644
--- a/lib/Cake/Utility/String.php
+++ b/lib/Cake/Utility/String.php
@@ -418,6 +418,46 @@ class String {
return preg_replace('|]+>|im', '', preg_replace('|<\/a>|im', '', $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.
*