Splitting Text into Pages
While no joke is likely to be so long that it will require more thanone page, many content-driven sites (like sitepoint.com) provide lengthy content
that is best presented when it's broken into pages. Yet another regular expression
function in PHP makes this exceedingly easy to do.split is a function that takes a regular expression and a string of
text, and uses matches for the regular expression to break the text into an
array. Consider the following example:
$regexp="[ ntr]+"; // One or more white space chars
$text="This is antest.";
$textarray=split($regexp,$text);
echo($textarray[0]."<br />"); // Outputs "This<br />"
echo($textarray[1]."<br />"); // Outputs "is<br />"
echo($textarray[2]."<br />"); // Outputs "a<br />"
echo($textarray[3]."<br />"); // Outputs "test.<br />"
As you might expect, there is also a spliti function that is
case insensitive.If we search for a [PAGEBREAK] tag instead of a white
space character, and we display only the page in which we're interested (indicated
by a $page variable passed with the page request, for example)
instead of all of the resulting portions of the text, we can successfully
divide our content into pages.[5]
// If no page specified, default to the
// first page ($page = 0)
if (!isset($_GET['page'])) $page = 0;
else $page = $_GET['page'];
// Split the text into an array of pages
$textarray=spliti("[PAGEBREAK]",$text);
// Select the page we want
$pagetext=$textarray[$page];
Obviously, we'll want to provide a way for users to move between pages.
Let's put a link to the previous page at the top of the current page, and
a link to the next page at the bottom.However, if this is the first page, clearly we won't need a link to
the previous page. We know we're on the first page if $page equals
zero.Likewise, we don't need a link to the next page on the last page of
content. To detect the last page, we need to use the count"A Content Management System". count takes an array, and returns the
number of elements in that array. When count is passed
our array of pages, it will tell us how many pages there are. If there are
10 pages, then $textarray[9] will contain the last page.
Thus, we know we're on the last page if $page equals count($textarray) minus
one.The code for the links that will turn our pages looks like this:
$PHP_SELF = $_SERVER['PHP_SELF'];
if ($page != 0) {
$prevpage = $page - 1;
echo("<p><a href="$PHP_SELF?id=$id&page=$prevpage">".
'Previous Page</a></p>');
}
// Output page content here...
if ($page < count($textarray) - 1) {
$nextpage = $page + 1;
echo("<p><a href="$PHP_SELF?id=$id&page=$nextpage">".
'Next Page</a></p>');
}
[5]The real reason for using regular
expressions here is to allow [PAGEBREAK] to be case insensitive;
that is, we want [pagebreak] or even [Pagebreak] to
work just as well. If you are happy with requiring the tag to be typed in
uppercase, you can actually use PHP's explode function instead.
It works just like split, but it searches for a specific
string rather than a pattern defined by a regular expression. Unlike str_replace (see "Matching Tags"), explode cannot accept an array
as its search argument. See the PHP
manual for details.