<?php

/*
http://px.sklar.com/code.html?code_id=63
By: Richard Lynch <lynch@lscorp.com>
*/

	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;
	}
?>
