note 28492 deleted from function.wordwrap by danbrown

From: Date: Sun, 17 May 2009 18:37:53 +0000
Subject: note 28492 deleted from function.wordwrap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155152@lists.php.net to get a copy of this message
Note Submitter: desolate19 at hotmail dot com ---- The wordwrap2 function posted by lteague is exactly what I was looking for. Unfortunatly it has an error. It will add the cut string any time a space is encountered outside of a tag since (!(0 % x)), where x != 0, is always true. Here's a fixed function with a few other improvments. Again this function will split a word that is longer that $cols and is outside any HTML tags by the string $cut. Lines with whitespace in them are ok, only single words over $cols lenght are split. function wordwrap2($str, $cols, $cut) { $len = strlen($str); $tag = 0; for ($i = 0; $i < $len; $i++) { $chr = $str[$i]; if ($chr == '<') { $tag++; } elseif ($chr == '>') { $tag--; } elseif ((!$tag) && (ctype_space($chr))) { $wordlen = 0; } elseif (!$tag) { $wordlen++; } if ((!$tag) && ($wordlen) && (!($wordlen % $cols))) { $chr .= $cut; } $result .= $chr; } return $result; }

« previous php.notes (#155152) next »