<?php
function textwrap($text, $wrap=80, $break='<BR>\n'){
$len = strlen($text);
if ($len > $wrap){
$h = '';
$lastWhite = 0;
$lastChar = 0;
$lastBreak = 0;
while ($lastChar < $len){
$char = substr($text, $lastChar, 1);
if (($lastChar - $lastBreak > $wrap) &&
($lastWhite > $lastBreak)){
$h .= substr($text, $lastBreak,
($lastWhite - $lastBreak)) . "$break";
$lastChar = $lastWhite + 1;
$lastBreak = $lastChar;
}
/* You may wish to include other characters as
valid whitespace... */
if ($char == ' ' || $char == chr(13) || $char
== chr(10)){
$lastWhite = $lastChar;
}
$lastChar = $lastChar + 1;
}
$h .= substr($text, $lastBreak);
}
else{
$h = $text;
}
return $h;
}
?>
<HTML>
<HEAD><TITLE>Textwrap Example</TITLE></HEAD>
<BODY>
<?php
$test = "This is a test. This is only a test. Had
this been a real emergency you would have been instructed to put your head
between your knees and pray.";
echo("$test<BR>\n<BR>\n");
for ($w = 80; $w > 0; $w = $w - 10){
$wrapped = textwrap($test, $w);
echo("$w<BR>\n$wrapped<BR>\n<BR>\n");
}
?>
</BODY>
</HTML>