A cool break_words function for PHP

I wrote this a while back, if you wrote a better one please let me know :)

             function break_words($text, $length, $result_length, $append) {
		preg_match_all('#([^ ]{' .$length .',})#', $text, $matches);
		foreach($matches[1] as $word) {
			$word = trim($word);
			$text = str_replace($word, substr($word, 0, $result_length) .$append, $text);
		}
		return $text;
	}

This function will break the long words in string $text, which are more than $length to be maximum $result_length characters and appends what ever you pass in as $append.

Example usage:

$post = break_words($post, 30, 30, '');

I’m not convinced that this is the best solution.

Leave a Comment for A cool break_words function for PHP