function str_trunc($str, $max, $strict = FALSE, $trunc = '')
{
    if (strlen($str) <= $max) {
        return $str;
    } else {
        if ($strict) {
            return substr($str, 0, strrposlimit($str, ' ', 0, $max + 1)) . $trunc;
        } else {
            $strloc = strpos($str, ' ', $max);
            if (strlen($strloc) != 0) {
                return substr($str, 0, $strloc) . $trunc;
            } else {
                return $str;
            }
        }
    }
}
示例#2
0
function str_trunc($str, $max, $strict = TRUE, $trunc = '')
{
    // Returns a trunctated version of $str up to $max chars, excluding $trunc.
    //Not written by me.
    // $strict = FALSE will allow longer strings to fit the last word.
    if (strlen($str) <= $max) {
        return $str;
    } else {
        if ($strict) {
            return substr($str, 0, strrposlimit($str, ' ', 0, $max + 1)) . $trunc;
        } else {
            $strloc = strpos($str, ' ', $max);
            if (strlen($strloc) != 0) {
                return substr($str, 0, $strloc) . $trunc;
            } else {
                return $str;
            }
        }
    }
}