|
Perforce Chronicle 2012.2/486814
API Documentation
|
Limit the length of a string and (optionally) append a trailing string when the length limit is exceeded. More...
Public Member Functions | |
| truncate ($input, $length, $trailing=null, $escapeOutput=true) | |
| Trims whitespace, truncates and appends given suffix if truncated. | |
Limit the length of a string and (optionally) append a trailing string when the length limit is exceeded.
| P4Cms_View_Helper_Truncate::truncate | ( | $ | input, |
| $ | length, | ||
| $ | trailing = null, |
||
| $ | escapeOutput = true |
||
| ) |
Trims whitespace, truncates and appends given suffix if truncated.
Truncates on word boundary provided the truncated string contains whitespace.
| string | $input | the string to truncate. |
| int | $length | the limit of the output string (excluding trailing string). |
| string | $trailing | optional - string to append if truncated. |
| bool | $escapeOutput | optional - if true (by default) then output will be escaped |
{
$input = trim($input);
$output = $input;
if (strlen($input) > $length) {
$output = substr($input, 0, $length);
if (preg_match('/\S/', $input[$length]) && preg_match('/\s/', $output)) {
$output = preg_replace('/\S+$/', '', $output);
}
$output = rtrim($output) . $trailing;
}
return $escapeOutput ? $this->view->escape($output) : $output;
}