Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_View_Helper_Truncate Class Reference

Limit the length of a string and (optionally) append a trailing string when the length limit is exceeded. More...

List of all members.

Public Member Functions

 truncate ($input, $length, $trailing=null, $escapeOutput=true)
 Trims whitespace, truncates and appends given suffix if truncated.

Detailed Description

Limit the length of a string and (optionally) append a trailing string when the length limit is exceeded.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

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.

Parameters:
string$inputthe string to truncate.
int$lengththe limit of the output string (excluding trailing string).
string$trailingoptional - string to append if truncated.
bool$escapeOutputoptional - if true (by default) then output will be escaped
Returns:
string the truncated string.
    {
        $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;
    }

The documentation for this class was generated from the following file: