Break word boundary

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
scd8418
Forum Newbie
Posts: 1
Joined: Sun Mar 09, 2008 11:17 pm

Break word boundary

Post 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;
}
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Break word boundary

Post 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);
    }
 
 
Post Reply