chunk_split();From: Benjamin Tr�panier
> I am using the chunk_split(); function to separe a
> long long text on differents pages.
> At this time, I can split the string into x sections of
> 2000 characters.
>
> Now I need to show only the first 2000 .
> when user click on page #2, it's reloading and
> show the next 2000 characters... If he click the page
> 4 button, it's going to the 6000th caracters and it show
> his next 2000.. etc...
>
> How can I split this text on many pages without loosing
> any characters?? Maybe the chunk_split function is not
> the good one...
You'll have to look for the spaces. So you want no more than 2000 characters
per page. If you're on page x, then start at character x*2000 and start
looping backwards character by character until you find a space. Then grab
the text up until that character. "remember" that characters location, too,
as you'll need to use that as your starting point on the next page.
Something like this:
$start = 0;
$chars = 2000;
$page = array();
$x = 0;
$len = strlen($long_text);
while($start < $len)
{
$spot = $start + $chars;
if($spot < $len)
{
while($long_text{$spot--} != ' ') { }
}
else
{ $spot = $len; }
$page[$x]['start'] = $start;
$page[$x]['chars'] = $spot - $start + 1;
$x++;
$start = $spot + 1;
}
Now you'll have your start and number of characters for each page. For
example to show page zero:
echo substr($long_text,$page[0]['start'],$page[0]['chars']);
To show page 5
echo substr($long_text,$page[5]['start'],$page[5]['chars']);
etc. Throw $page into a session variable so you only have to calculate it
once.
Also, if you're pulling this text from a database or file, combine it with
the database and/or file functions so that you only extract/read the part of
the text your after instead of the whole text each time.
Hope that helps.
---John Holmes...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php