Back to PhocacartText class

Method truncateText

public static
truncateText
(mixed $text, mixed $limit, mixed $suffix = '')

Method truncateText - Source code

public static function truncateText($text, $limit, $suffix = '')
{
    $length = StringHelper::strlen($text);
    if ((int) $length < (int) $limit) {
        return $text;
    }
    $textOnlySpaces = preg_replace('/\\s+/', ' ', $text);
    $textTruncated = StringHelper::substr($textOnlySpaces, 0, StringHelper::strpos($textOnlySpaces, " ", $limit));
    $textOutput = trim(StringHelper::substr($textTruncated, 0, StringHelper::strrpos($textTruncated, " ")));
    return $textOutput . $suffix;
}