Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I tried to do pagination of an article with an arbitrary 2500 characters per  
page but if
the cutoff point is not the end of a paragraph (<br><br>), it would be moved
there instead. I am using php5. Here is the skeleton of my code:

$length = strlen($article);
$charsPerPage = 2500;
$numberOfPages = ceil($length/$charsPerPage);
$start = 0;

for($counter=1;counter<$numberOfPages + 1;$counter++) {
        $cutOffPoint= strripos(substr($all, $start, $charsPerPage),  
       $pageContent = substr($article, $start, $cutOffPoint-$start);
       $start = $cutOffPoint;
       echo "page".counter." = ".$pageContent;

However, it only works for the first loop for some reason. In the
subsequent loop the $cutOffPoint is located even before the $start (visible  
when I
print them out for testing purpose). Could someone show me what I am  

Re: pagination

Peter Parker wrote:

Quoted text here. Click to load it

This is plain false...

Quoted text here. Click to load it

$cutOffPoint is relative to "substr($all, $start, $charsPerPage)", not
relative to $article. By the way, why do you have $all and $article??

Besides that, your algorithm doesn't take into account extreme cases: what
if I have no "<br>" at all in my article? What if my article is nothing but
a lot of "<br>"s?

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"Only sick music makes money today."
  --  Friedrich Nietzsche [1844 - 1900]

Re: pagination

Sorry, it should have been '$article' instead of '$all'. I renamed and  
missed that one when I posted onto newsgroup. The article format is created  
by me so I make sure the format holds. I did print it out to verify. It  
contains only text strings in English language with single newline as <br>  
and double newlines for new paragraph as <br><br>. Thanks.

Quoted text here. Click to load it

Site Timeline