Commit 8d18d722 by Alexander Makarov

Merge branch 'stringhelper-truncate' of github.com:Alex-Code/yii2 into…

Merge branch 'stringhelper-truncate' of github.com:Alex-Code/yii2 into Alex-Code-stringhelper-truncate Conflicts: framework/CHANGELOG.md
parents 5c4406b7 46f381da
......@@ -25,6 +25,7 @@ Yii Framework 2 Change Log
- Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue)
- Enh #2837: Error page now shows arguments in stack trace method calls (samdark)
- Enh #2906: Added support for using conditional comments for js and css files registered through asset bundles and Html helper (exromany, qiangxue)
- Enh #2942: Added truncate and truncateWord methods (Alex-Code)
- Enh #3008: Added `Html::errorSummary()` (qiangxue)
- Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue)
- Enh #3103: debugger panel is now not displayed when printing a page (githubjeka)
......
......@@ -87,4 +87,38 @@ class BaseStringHelper
return '';
}
}
/**
* Truncates a string to the specified length.
* @param string $string The string to truncate.
* @param integer $length The new length of the string.
* @param string $suffix A value to affix to the end.
* @param string $encoding The charset to use, defaults to application current.
* @return string the truncated string.
*/
public static function truncate($string, $length, $suffix = '...', $encoding = null)
{
if (mb_strlen($string, $encoding ?: \Yii::$app->charset) > $length) {
return trim(mb_substr($string, 0, $length, $encoding ?: \Yii::$app->charset)) . $suffix;
} else {
return $string;
}
}
/**
* Split a string into words preserving whitespace and return the specified amount of words.
* @param string $string The string to truncate.
* @param integer $count How many words to truncate to.
* @param string $suffix A value to affix to the end.
* @return string the truncated string.
*/
public static function truncateWords($string, $count, $suffix = '...')
{
$words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
if (count($words) / 2 > $count) {
return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
} else {
return $string;
}
}
}
......@@ -65,4 +65,19 @@ class StringHelperTest extends TestCase
$this->assertEquals('foo', StringHelper::basename('/bar/foo/'));
$this->assertEquals('foo', StringHelper::basename('\\bar\\foo\\'));
}
public function testTruncate()
{
$this->assertEquals('string test', StringHelper::truncate('string test', 20));
$this->assertEquals('string...', StringHelper::truncate('string test', 6));
$this->assertEquals('string!!!', StringHelper::truncate('string test', 6, '!!!'));
}
public function testTruncateWords()
{
$this->assertEquals('this is a string test', StringHelper::truncateWords('this is a string test', 5));
$this->assertEquals('this is a string...', StringHelper::truncateWords('this is a string test', 4));
$this->assertEquals('this is a string!!!', StringHelper::truncateWords('this is a string test', 4, '!!!'));
$this->assertEquals('this is a big space...', StringHelper::truncateWords('this is a big space string', 5));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment