note 41564 deleted from function.wordwrap by aidan

From: Date: Mon, 19 Jul 2004 23:25:05 +0000
Subject: note 41564 deleted from function.wordwrap by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73643@lists.php.net to get a copy of this message
Note Submitter: Orbitz@vietmartyrs.net cttdvnpa.org ---- I have modified the wordwrap3 function //////////// $white_space = ' '; if($str[$i] !=$white_space) $segment_width++; //////// The reason for this: I was working a modication of the invision board forums. I needed to prevent user post a post with a very long string with no whitespace; with the original function wordwrap3(), this function does just fine; however, it will effect the rest of other posts of other users who have replied to that post ..and it makes their posts not to use the rest of the space available on a single line. This was because segnment_width also included the white space in the original version of the function. Solution: i skipped i donot increament the segnment_width when there is a white space. function wordwrap3($str,$cols,$cut) { $tag_open = '<'; $tag_close = '>'; $white_space = ' '; $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) { if($str[$i] !=$white_space) $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; }

« previous php.notes (#73643) next »