note 2263 modified in String functions by ronabop

From: Date: Fri, 11 Aug 2000 16:59:53 +0000
Subject: note 2263 modified in String functions by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-1309@lists.php.net to get a copy of this message
A quick and dirty function to wrap text at a given wrap margin: <p> function wraptext($text, $wrapmargin) { $rettext = ""; $linebuf = ""; $linelen = 0; $tok = split("[ \t]", $text); $numtok = count($tok); for ($i = 0; $i < $numtok; $i++) { $elem = $tok[$i]; $elemlength = strlen($elem) + 1; if ($linelen + $elemlength > $wrapmargin) { $rettext = $rettext . $linebuf . "\n"; $linebuf = ""; $linelen = 0; } // Do we have a newline in this element? $pos = strrpos($elem, "\n"); if ($pos) { $before = substr($elem, 0, $pos); $after = substr($elem, $pos); $rettext = $rettext . $linebuf . " " . $before . "\n"; $linebuf = $after; $linelen = strlen($after); } else { $linebuf = $linebuf . " " . $elem; $linelen = $linelen + $elemlength; } } // Do we have a newline in this element? $pos = strrpos($elem, "\n"); if ($pos) { $before = substr($elem, 0, $pos); $after = substr($elem, $pos); $rettext = $rettext . $linebuf . " " . $before . "\n"; $linebuf = $after; $linelen = strlen($after); } else { $linebuf = $linebuf . " " . $elem; $linelen = $linelen + $elemlength; } } return $rettext; } http://www.php.net/manual/string-functions.php

« previous php.notes (#1309) next »