note 28492 added to function.wordwrap
| From: | desolate19 at rack1 dot php dot net | Date: | Tue, 14 Jan 2003 21:18:32 +0000 |
| Subject: | note 28492 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42143@lists.php.net to get a copy of this message | ||
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;
}
--
http://www.php.net/manual/en/function.wordwrap.php
http://master.php.net/manage/user-notes.php?action=edit+28492
http://master.php.net/manage/user-notes.php?action=delete+28492
http://master.php.net/manage/user-notes.php?action=reject+28492