khanat-code-old/code/web/public_php/admin/smarty/plugins/modifier.truncate.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2010-05-06 00:08:41 +00:00
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty truncate modifier plugin
*
* Type: modifier<br>
* Name: truncate<br>
* Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word, and
2014-09-13 20:09:48 +00:00
* appending the $etc string or inserting $etc into the middle.
2010-05-06 00:08:41 +00:00
* @link http://smarty.php.net/manual/en/language.modifier.truncate.php
* truncate (Smarty online manual)
2014-09-13 20:09:48 +00:00
* @author Monte Ohrt <monte at ohrt dot com>
2010-05-06 00:08:41 +00:00
* @param string
* @param integer
* @param string
* @param boolean
2014-09-13 20:09:48 +00:00
* @param boolean
2010-05-06 00:08:41 +00:00
* @return string
*/
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
2014-09-13 20:09:48 +00:00
$break_words = false, $middle = false)
2010-05-06 00:08:41 +00:00
{
if ($length == 0)
return '';
if (strlen($string) > $length) {
2014-09-13 20:09:48 +00:00
$length -= min($length, strlen($etc));
if (!$break_words && !$middle) {
2010-05-06 00:08:41 +00:00
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
2014-09-13 20:09:48 +00:00
}
if(!$middle) {
return substr($string, 0, $length) . $etc;
} else {
return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
}
} else {
2010-05-06 00:08:41 +00:00
return $string;
2014-09-13 20:09:48 +00:00
}
2010-05-06 00:08:41 +00:00
}
/* vim: set expandtab: */
?>