note 40592 added to function.wordwrap
| From: | dawildorcinator at yahoo dot no dot spam dot please dot com | Date: | Tue, 09 Mar 2004 04:05:25 +0000 |
| Subject: | note 40592 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66466@lists.php.net to get a copy of this message | ||
Here's a kinder more gentle word wrap that i've modified from the previous posts. This
one will look for a "Previous" white space before it tries to hack a line in half.
function wordwrap3($str,$cols,$cut) {
$tag_open = '<';
$tag_close = '>';
$count = 0;
$in_tag = 0;
$str_len = strlen($str);
$segment_width = 0;
$last_white_space = 0;
$begin_of_line = 0;
for ($i=0 ; $i<=$str_len ; $i++){
if ($str[$i] == $tag_open) {
$in_tag++;
} else {
if ($str[$i] == $tag_close) {
if ($in_tag > 0) {
$in_tag--;
}
} else {
if ($in_tag == 0) {
$segment_width++;
if ($segment_width > $cols) {
if ($str[$i] == " ") {
$str = substr($str,0,$i).$cut.substr($str,$i+1,$str_len-1);
$i += strlen($cut);
$str_len = strlen($str);
$segment_width = 0;
$begin_of_line = $i;
$last_white_space = $i;
} else {
if ($last_white_space != $begin_of_line) {
$i = $last_white_space;
}
$str = substr($str,0,$i+1).$cut.substr($str,$i+1,$str_len-1);
$i += strlen($cut);
$str_len = strlen($str);
$segment_width = 0;
$begin_of_line = $i;
$last_white_space = $i;
}
} else {
if ($str[$i] == " ") {
$last_white_space = $i;
}
}
}
}
}
}
return $str;
}
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40592
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40592&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40592&report=yes
Search -- http://master.php.net/manage/user-notes.php