note 41564 added to function.wordwrap

From: Date: Wed, 14 Apr 2004 21:53:51 +0000
Subject: note 41564 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-68136@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41564 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41564&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41564&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68136) next »