Am working on a php/mysql project that involves displaying long piece of text on a page. The text being that long, I would like a way of splitting up this article into multiple pages that will give my project the ease of navigating through.
Any help would be highly appreciated. Rgds.

7 Years
Discussion Span
Last Post by antiv_boy

sorry if i didnt make myself clear in the first instance. The text being displayed on the website is read dynamically from a mysql db. This is basically a news website. I would like to split long articles into smaller easier to read chnks without having to scroll through a very long page. Hope this makes it clearer. Rgds.


I would personally make a break tag to insert into my posts and use explode to separate the pages:

$page = $_GET['page']; //so the script knows what part of the post to display
$page--; //decrement page since the array is zero indexed.
$break = "[break]"; //whatever you want to break it up with;

$getPages = explode('[break]',$content); //content is the blog post
$chunk = $getPages[$page];
echo $chunk;

You will also have to get rid of the '[break]' with str_replace. But now you can display a piece of content like: http://www.site.com/post.php?page=1 and it will show the first piece of the post before the '[break]'.

Edited by xylude: n/a


In my case, am working with data that already exists ina db. I managed to get some headway by splitting at </p> positions after certain number of charcters. my code basically searches for the nearest </p> between a floor and ceiling value e.g. between the 2500th to 3000th character and it marks that as the end position for my split and the start position for the next range of text split.

@chrishea, thanks, ithink i cud employ your code very usefully for other sections on my project. My immediate concern now is to have a page that registers pageviews on button click on the link.

My problem now is with the location of the </p> tag.

Why is the code below not locating a </p> tag?
strpos($maintext1,"</p>", $startsearch)



hi guys once again,
The code is here:

 $articleq1 = "SELECT maintext FROM eas_wfs_article WHERE articleid=$articleid";
 $result1 = mysql_query($articleq1);
 $articlerec1 = mysql_fetch_array($result1);
 $maintext1 = $articlerec1['maintext'];
 //find out where we are before outputting the page

 if ($articlepage >= 1) { $endsearch = $articlepage * 4000; $startsearch = $endsearch-500; }

 if ((strlen($maintext1) > $endsearch) && (strlen($maintext1) > 1)) {
      $paragraphposition = strpos($maintext1,"</p>", $startsearch)-1; //search for position of last paragraph

 if ($articlepage > 1) { 
      $startsearch = $startsearch-4000;
      $startposition = strpos($maintext1,"</p>", $startsearch)-1; 

 if ($articlepage == 1) { $startposition = 0; }    //on first page, start reading from beginning of article

 $strlength = $paragraphposition - $startposition; 

 if (!$paragraphposition) { //are we at the end i.e. no more paragraphs?
      $strlength = strlen($maintext1) - $startposition; 

Then I use the $strlength and $startposition as follows:

  $maintext = substr($maintext,$startposition, $strlength);

My code is splitting the text at the occurence of a p and not a </p>.
Do I need to escape the </ and >? If so, how do I go about it? Your assistance will be highly appreciated.

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.