Title: Word Wrapping Function
Contributor: Dominic J. Eidson (aka `Sauron)
Last Update: Tuesday June 20 05:19 EDT 2000
<?
/* word_wrap($string, $cols, $prefix)
*
* Takes $string, and wraps it on a per-word boundary (does not clip
* words), no more than $cols per line. Allows for optional prefix string
* for each line. (Was written to easily format replies to e-mails,
* prefixing each line with "> ".
*
* Copyright 1999 Dominic J. Eidson, use as you wish, but give credit
* where credit due.
*/
function wrap_text ($string, $cols, $prefix) {
$t_lines = split( "\n", $string);
$outlines = "";
while(list($foo, $thisline) = each($t_lines)) {
if(strlen($thisline) > $cols) {
$newline = "";
$t_l_lines = split(" ", $thisline);
while(list($bar, $thisword) = each($t_l_lines)) {
if((strlen($newline) + strlen($thisword)) > $cols) {
$outlines .= $prefix.$newline."\n";
$newline = $thisword." ";
} else {
$newline .= $thisword." ";
}
}
$outlines .= $prefix.$newline."\n";
} else {
$outlines .= $prefix.$thisline."\n";
}
}
return $outlines;
}
?>
Anyone who wishes to make additions or changes to this
PHP Tip email them to webmaster@linuxguruz.org
This document is Copyright (c) 1999 by LinuxGuruz