note 87351 deleted from function.str-split by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Dec 2008 18:43:25 +0000 |
| Subject: | note 87351 deleted from function.str-split by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147489@lists.php.net to get a copy of this message | ||
Note Submitter: jesus at dunlock dot com
----
/* this one is working well
*
* @param $text to cut in pieces
* @param $n piece length
* @param(input/output) $ttab array object by reference
* @return $p pieces/pages
*
* by Xus; enjoy it!
*/
function divide_text($text, $n, &$ttab)
{
$ttab = array(); //array dividit
//removing spaces from
$text=ltrim(rtrim($text));
$l = strlen($text); // text length
$first = 0;
$p=0;
if ($l <= $n)
{
$ttab[] = $text;
return 1;
} else
{
while(strlen($text)>$n)
{
$tmp = substr($text, $first, $n);
// we're looking for last space in substring
$lastpos = strrpos($tmp," ");
//prevention from spaces between words
$ttab[] = rtrim(substr($text, $first, $lastpos));
$text=ltrim(substr($text, $lastpos, $l));
$p++;
}
$ttab[] = $text;
}
return $p;
}