<?php
/*
* In this case, $str is the string, $n is the max number of characters per
* line, $insert is what to put in between each line, $max is the maximum
* number of lines to return, $conserve will only break lines at the $break
* character.
*
* $foo = "One two three four five six seven eight nine ten.";
* $x = SplitIntoLines($foo, 38, "\n", 0, True, " ");
* echo $x;
*
* Source: e-gineer - PHP Knowledge Base <http://e-gineer.com/phpkb/>
*
* $Id: strings.inc.php,v 1.6 2001/10/16 02:45:16 zaiborg Exp $
*/
if (defined('__strings_inc')) return;
define('__strings_inc', 1 );
if (DEBUG) { echo "<!-- Start of file: strings -->\n"; }
function SplitIntoLines ($str, $n, $insert, $max=0, $conserve=False, $break=" ") {
static $count = 0;
$count++;
if ($max > 0) {
if ($count >= $max) {
return $str;
}
}
$retval = False;
if (strlen($str) <= $n) {
$retval = $str;
} else {
if (!$conserve) {
$begin = substr($str, 0, $n);
$end = substr($str, $n, strlen($str) - $n);
$retval = $begin . $insert . SplitIntoLines($end, $n, $insert, $max);
} else {
$begin = substr($str, 0, $n);
$pos = strrpos($begin, $break);
if ($pos >= 1) {
$begin = substr($str, 0, $pos);
$end = substr($str, $pos+1, strlen($str) - $pos + 1);
$retval = $begin . $insert . SplitIntoLines($end, $n, $insert, $max, $conserve, $break);
}
}
}
return $retval;
}
/**
* Description:
*/
function splitString($text, $length=80, $joinAll=false)
{
if ($joinAll) { eregi_replace("\n", " ", $text); }
$paragraphs = explode("\n",$text);
$max = count($paragraphs);
for ( $i = 0; $i < $max; $i++ )
{
$tmpText = stripslashes($paragraphs[$i]);
while ( strlen($tmpText) > $length )
{
// Find the position of the last space within the first $length chars:
$splitAt = strrpos(substr($tmpText,0,$length), " ");
$lines[] = substr($tmpText,0,$splitAt);
$tmpText = substr($tmpText,$splitAt+1);
}
$lines[] = $tmpText; // Remember the last chunk!!!
}
return $lines;
}
if (DEBUG) { echo "<!-- End of file: strings -->\n"; }
?>