note 35828 added to function.wordwrap

From: Date: Tue, 16 Sep 2003 08:23:21 +0000
Subject: note 35828 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-56560@lists.php.net to get a copy of this message
Hi @ll I have modify one of the previews posted functions. This version have the wordwrap option [cut], so it split no words, which are not longer the max-char-lenght. This function doesn't cut in HTML tags, just in the plain text. function short_words($str,$cols,$cut) { $str = str_replace("\r\n","",$str); $str = str_replace("\n","",$str); $str = str_replace("\r","",$str); $tag_open = '<'; $tag_close = '>'; $in_tag = 0; $str_len = strlen($str); $segment_width = 0; for ($i=0; $i <= $str_len; $i++){ if (substr($str,$i,strlen($cut)) == $cut) { $i += strlen($cut)-1; $segment_width = 0; } else { 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 ($str[$i] == " ") { $last_space = $i; } if (($segment_width > $cols) && ($str[$i] != " ")) { if ($last_space < $i) { $str = substr($str,0,$last_space).$cut.substr($str,$last_space); $i = $last_space + strlen($cut) - 1; } else { $str = substr($str,0,$i).$cut.substr($str,$i); $i += strlen($cut) - 1; } $last_space = 0; $str_len = strlen($str); $segment_width = 0; } } } } } return $str; } $str = short_words($str, 45 ,"<br />"); Cu Nev ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35828 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35828&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35828&report=yes

« previous php.notes (#56560) next »