note 25593 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:38:33 +0000 |
| Subject: | note 25593 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155157@lists.php.net to get a copy of this message | ||
Note Submitter: info-at-oxyscripts-dot-com
----
Jeffrey Sambells code above has a few errors in it that prevented it working properly, which I have
changed so that it works as desired. It will wrap a string properly, and exclude HTML from wrapping.
function even_better_wordwrap($str,$cols,$cut) {
$tag_open = '<';
$tag_close = '>';
$count = 0;
$in_tag = 0;
$str_len = strlen($str);
$segment_width = 0;
for ($i=0 ; $i<=$str_len ; $i++){
if ($str[$i] == $tag_open) {
$in_tag++;
} elseif ($str[$i] == $tag_close) {
if ($in_tag > 0) {
$in_tag--;
}
} else {
if ($in_tag == 0) {
$segment_width++;
if (($segment_width > $cols) && ($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;
}
}
}
}
return $str;
}
Alex
http://www.oxyscripts.com