Putting it all Together
The completed code that will output our joke text (with all specialcharacter, multi-page, and custom tag conversion in place) is as follows.
This file, along with an updated joke listing script (jokelist.php),
and a front page that lets our visitors choose a joke category to view (index.php),
are provided in the code archive.
<!-- joke.php -->
...
// Get the joke text from the database
$id = $_GET['id'];
$joke = mysql_query("SELECT JokeText FROM Jokes
WHERE ID='$id'");
$joke = mysql_fetch_array($joke);
$joketext = $joke['JokeText'];
// Filter out HTML code
$joketext = htmlspecialchars($joketext);
// 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]',$joketext);
// Select the page we want
$joketext=$textarray[$page];
// Bold and italics
$joketext = str_replace(
array('[b]','[B]'),'<strong>',$joketext);
$joketext = str_replace(
array('[eb]','[EB]'),'</strong>',$joketext);
$joketext = str_replace(
array('[i]','[I]'),'<em>',$joketext);
$joketext = str_replace(
array('[ei]','[EI]'),'</em>',$joketext);
// Paragraphs and line breaks
$joketext = ereg_replace("r",'',$joketext);
$joketext = ereg_replace("nn",'</p><p>',$joketext);
$joketext = ereg_replace("n",'<br />',$joketext);
// Hyperlinks
$joketext = ereg_replace(
'[L]([-_./a-zA-Z0-9!&%#?+,'=:~]+)[EL]',
'<a href="></a>', $joketext);
$joketext = ereg_replace(
'[L=([-_./a-zA-Z0-9!&%#?+,'=:~]+)]'.
'([-_./a-zA-Z0-9 !&%#?+$,'"=:;~]+)[EL]',
'<a href=">2</a>', $joketext);
$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>');
}
echo( "<p>$joketext</p>" );
if ($page < count($textarray) - 1) {
$nextpage = $page + 1;
echo("<p><a href="$PHP_SELF?id=$id&page=$nextpage">".
'Next Page</a></p>');
}
...
Don't forget to provide documentation so that users of your joke submission
form know what tags are available and what each of them does.