php breakup long lines

function breakUpLongLines( $text, $maxLength=80 )
{
$counter = 0;
$newText = '';
$array = array();

$textLength = strlen( $text );

for( $i = 0; $i <= $textLength; $i++ )
{
$array[] = substr( $text, $i, 1 );
}

$textLength = count( $array );

for( $x = 0; $x < $textLength; $x++ )
{
if( preg_match( "/[[:space:]]/", $array[ $x ] ) )
{
$counter = 0;
}
else
{
$counter++;
}

$newText .= $array[ $x ];

if( $counter >= $maxLength )
{
$newText .= ' ';

$counter = 0;
}
}

return $newText;
}

source

Leave a Reply