Need help breaking up text for multiple pages

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

Threaded View
I was recently put in charge of heading up my company's website, and
while I have a lot of experience on the design side of things, I'm
still very new to the programming side. When I started, the website had
just gotten a revision, but the site was an utter mess, so I've been
trying to fix it up. As I've gone along, I've learned some aspects of
PHP, but my knowledge is still very limited.

Here is the problem I'm trying to fix. A backend is used to enter
information into our MySQL database, and then a page template pulls it
out to make the page. For our reviews, the PHP coding currently breaks
up the review's text so that it can be places across multiple pages.
The problem is, the way the text breaking was set up, the end of one
page and the beginning of the next are totally chaotic, with breaks
often coming mid-sentence.

Here is what I'd love: other coding automatically puts an HTML break at
the end of paragraphs, and then another in the space between one
paragraph and the next. I'd love to have the code search for when those
double breaks come up, and then break up the text after, say, every
sixth pair of HTML breaks. I'd also like the option to break the text
myself by putting in some sort of text code in the original text. At
this point, though, anything that can make the breaks more elegant
would be a huge improvement.

I really appreciate any help anybody can give me in this one. Please
remember that I know very little of PHP, and I'm not much of a
programmer anyhow, so things that you may take for granted that people
would understand about PHP, I might not know.

I tried to pull out what I thought was the current coding for the text
break. Here it is. I'd also like to get rid of the part1/part2 factor.
Before, they had the text breaking to help in fitting into the layout
around an image, but I've fixed that so I no longer need the break. So,
that portion no longer needs to be a factor.

if ($page == ""){
    $page = 1;
$no_letters = strlen($Game_Full_Story);
$times = ($no_letters / 3800) +1;
$Game_Full_Story = wordwrap($Game_Full_Story,3800,"*#^");
$Game_Full_Story = explode ("*#^", $Game_Full_Story);
$pages = array();
for ($i=0; $i<$times ; $i++){
    $part[$i] = $Game_Full_Story[$i];
    if ($i != 0){
        array_push($pages, $i);
$page = $page-1;
$story = $part[$page];
if (strlen($story) < 460){
    $parts1 = $story;
    $story = wordwrap($story,460,"*#^");
    $story = explode ("*#^", $story);
    $parts1 = $story[0];
    $count = count($story);
    for ($i=1; $i<$count; $i++){
        $parts2 .=$story[$i];
        $parts2 .=" ";

Re: Need help breaking up text for multiple pages

I'm a bit confused on the purpose of

          $page = $page -1;

Aren't we moving from page 1 to page N? Shouldn't that be a $page++?

You mentioned you wanted breaks after each paragraph. I'm how that
that's what you else should look like in order to accomplish this.


        $story = wordwrap($story,460,"*#^");
        $story = explode ("*#^", $story);
        $parts1 = $story[0];
        $count = count($story);
        $parts2="<BR>"; //inserted break here
        for ($i=1; $i<$count; $i++){
                $parts2 .=$story[$i];
                $parts2 .="<BR>";//inserted break here

Could you also provide a sample of what's in your database. So as i
could understand what's being worked with and what format the data is

Re: Need help breaking up text for multiple pages

First, I have no idea why that code is there, as I didn't put it there
myself. *heh*

Actually, let me clear up what I was saying. Right now, something
somewhere in the coding of the site puts in <br/> tags for every
carrier return. (More on that in a moment.) So, my thought was, instead
of breaking up the text for separate pages at an arbitrary place, and
possibly cut a paragraph or sentence in half, I was thinking that if I
could have the PHP code break up the text at, say, every sixth grouping
of two <br/>s together, instead of say at X number of words or
characters. That way, each page would have like six paragraphs on it,
but that would also make it so that if I have an interview with a
question, one <br/>, and then the answer, it wouldn't break up the
question and answer.

Does that make sense?

As far as what is in the database, it's just plain old text. For
example, in the backend, I'll cut the review from my text editor, and
then paste it in. Either before or after it goes into the database, it
gets the <br/> tags added to it. (If it is important which step it is,
I can check.)

Re: Need help breaking up text for multiple pages

if there's already <br> in the database i don't think there should be a
need to do that in your code. What you could do is remove all breaks in
the database and let the code do that for you.

Re: Need help breaking up text for multiple pages wrote:
Quoted text here. Click to load it

Well, could it be as simple as using strpos() to find the <br>s?  (Actually in  
this case stripos() would be better because it's not case sensitive) (Warning -  
NOT tested!)

   if (!isset($page) || $page == "")
      $page = 1;

   $pos = 0;
   for ($cpage = 0; $cpage < $page; $cpage++) {
     $pstart = $pos;
     for ($cpar = 0; $cpar < 6; $cpar++) {  // Change 6 to # of paras/page
       $oldpos = $pos;
       $pos = stripos($text, '<br>', $pos);
       if ($pos === false)   // no more found
         $pos++;  // Point to next char and continue
   if ($oldpos > $pstart)
     echo substr($text, $pstart, $oldpos);
     echo "End of article";

Probably not perfect, but it should get you started.

Alternatively, you could split the paragraphs into an array and just count the  
number of items you need, i.e. (Again no tested)

   if (!isset($page) || $page == "")
      $page = 1;
   $tarray = preg_split('/<br>/i', $text);
   $pcount = 6;                // Number of paragraphs per page
   $startpar = ($page-1) * $pcount;
   $endpar = min($startpar + $pcount, count($tarray));  // Don't go past end
   for ($i = $startpar; $i < $endpar; $i+)
     echo $tarray[$i];

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline