You Are Here Home > A cool break_words function for PHP
DirectorySync is a directory synchronizing and backup utility providing automated, real-time syncing and scheduled, configurable backups at an affordable price.

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.

A cool break_words function for PHP
Filed under: PHP,Web Development   Posted by: Codehead
Do you have any questions? ask here.




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment