note 4526 deleted from ref.strings by sander

From: Date: Sun, 07 Apr 2002 13:35:56 +0000
Subject: note 4526 deleted from ref.strings by sander
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-28912@lists.php.net to get a copy of this message
This is an adjustment of ptomblin's code (ptomblin@xcski.com) for wrapping text to a certain number of characters. e.g. from within a mailing form. I couldn't get the original to work so changed it as follows. It puts a space put at the beginning of each line - but that's not too ugly. function format_mail($text, $wrapmargin){ $rettext = ""; $linebuf = ""; $linelen = 0; // split the text into lines (the main difference to the original) $lines = split("[\n]", $text); $numlines = count($lines); for ($j = 0; $j < $numlines; $j++) { // split the lines into words $tok = split("[ \t]", $lines[$j]); $numtok = count($tok); for ($i = 0; $i < $numtok; $i++) { $elem = $tok[$i]; $elemlength = strlen($elem) + 1; if ($linelen + $elemlength > $wrapmargin) { // the next word should wrap so add current line to the return $rettext = $rettext . $linebuf . "\n"; // reset buffer $linebuf = ""; $linelen = 0; } // add the next word to the current (or new) line buffer $linebuf = $linebuf . " " . $elem; $linelen = $linelen + $elemlength; } // end of the line - add it to the return $rettext = $rettext . $linebuf . "\n"; // reset buffer $linebuf = ""; $linelen = 0; } return $rettext; }

« previous php.notes (#28912) next »