Mr. Heyes more or less prompted me to go dig for my other, slightly
heavier version, that doesn't chop words up:

Sorry I hit Reply instead Reply All. Regardless, here's my str_curtail. There is a bug in it that means if the string is all one word then it gets curtailed to nada, but that's easily fixed.

    /**
    * Shortens the given string to the specified number of characters,
    * however will never shorten mid-word (going backwards to find white
    * space). Appends
    * "..." (unless third arg is given).
    *
    * @param  string $str    Input to shorten
    * @param  int    $length Length to shorten to (defaults to 35)
    * @param  string $append String to append (defaults to "...")
    * @return string         Resulting shortened string
    */
    function str_curtail($str, $length = 35, $append = '...')
    {
        // String short enough already ?
        if (strlen($str) <= $length) {
            return $str;
        }

        $str = substr($str, 0, $length);

        // No body intentionally
        for ($i=$length - 1; !ctype_space($str{$i}) && $i > 0; --$i);

        return rtrim(substr($str, 0, $i)) . $append;
    }

--
Richard Heyes
Employ me:
http://www.phpguru.org/cv

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to