Page 1 of 1

Break word boundary

Posted: Sun Mar 09, 2008 11:24 pm
by scd8418
Quite often I need to break long strings of text on word boundaries. I have a perl function that does this nicely with regex, but it seems to fail in php despite numerous tries to make it work. As a result, I came up with the function below. It seems to work and I thought the world needed to know.

static public function shortenWordBoundary($h, $m) {
if (strlen($h) <= $m) return $h;
$h = substr($h, 0, $m+1);
$n = preg_replace("/^(.+)\s.*/s", '$1', $h);
if ($n) return "$n...";
else return $h;
}

Re: Break word boundary

Posted: Wed Mar 12, 2008 7:25 am
by GeertDD
Hmm, I created a similar function for the text helper of Kohana. My approach differs a bit though. So, for educational purposes, here it is:

Code: Select all

<?php // http://trac.kohanaphp.com/browser/trunk ... v=2286#L14
 
    /**
     * Limits a phrase to a given number of words.
     *
     * @param   string   phrase to limit words of
     * @param   integer  number of words to limit to
     * @param   string   end character or entity
     * @return  string
     */
    public static function limit_words($str, $limit = 100, $end_char = NULL)
    {
        $limit = (int) $limit;
        $end_char = ($end_char === NULL) ? '&#8230;' : $end_char;
 
        if (trim($str) === '')
            return $str;
 
        if ($limit <= 0)
            return $end_char;
 
        preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
 
        // Only attach the end character if the matched string is shorter
        // than the starting string.
        return rtrim($matches[0]).(strlen($matches[0]) === strlen($str) ? '' : $end_char);
    }