Skip to content
Permalink
Browse files

String::tail()

  • Loading branch information...
dereuromark committed Aug 2, 2012
1 parent 22c1ac9 commit 917d912a43d1b7d22b6b86a60d8904cbc70490a7
Showing with 80 additions and 0 deletions.
  1. +40 −0 lib/Cake/Test/Case/Utility/StringTest.php
  2. +40 −0 lib/Cake/Utility/String.php
@@ -443,6 +443,46 @@ public function testTruncate() {
$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
*
@@ -418,6 +418,46 @@ public static function stripLinks($text) {
return preg_replace('|<a\s+[^>]+>|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.
*

0 comments on commit 917d912

Please sign in to comment.
You can’t perform that action at this time.