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.

Dani AI

Generated

A few likely causes and straightforward fixes.

If the DB field contains literal HTML closing tags (</p>) then searching for the HTML-entity string (&lt;/p&gt;) will never match, and vice‑versa if the text was HTML‑escaped before storage or output. Also note that strpos() returns the integer 0 when a match occurs at the very start and boolean false when not found. Tests like if (!$pos) will treat a valid 0 position as “not found.” Subtracting 1 from a false result yields -1 and breaks offsets. A mistaken variable name (for example using $maintext instead of $maintext1) will also produce odd behavior.

A safer approach is to search for the exact token, test for === false, and compute the end offset by adding strlen($needle) rather than subtracting 1. Example:

<?php
$needle = '</p>';
$pos = strpos($maintext1, $needle, $startSearch);
if ($pos === false) {
    $endPos = strlen($maintext1);
} else {
    $endPos = $pos + strlen($needle);
}
$length = $endPos - $startPos;
$chunk = substr($maintext1, $startPos, $length);
?>

For robustness with real HTML, consider parsing with DOMDocument or splitting by </p> with preg_split('/<\/p>/i'). If the editor can insert markers, ’s break‑token method is simplest; if not, DOM parsing handles malformed markup better. Sanitize the page input (cast to int), guard bounds, prefer PDO/MySQLi over deprecated mysql_* calls, and record pageviews server‑side (page load or an AJAX endpoint) rather than relying on fragile client events.

Recommended Answers

All 6 Replies

If the text is created in advance, why not convert it to PDF and let the adobe plug-in do the work? If the text is created dynamically you still have the option of creating a PDF from it (in PHP) and then display that.

If that doesn't do it for you, try a search on php pagination. You'll find some options available including this one:

http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

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:

<?php
$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]'.

Here is a link to something a little bit fancier if that's what you want:
virtual pagination

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.

, 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)
...

Thsnks.
Thanks.

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.
Rgds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.