regular expressions question v.pagebreak

Discussion in 'OT Technology' started by D1G1T4L, May 31, 2007.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    in one of the php/mysql tutorials i am reading there is a line that goes like this
    Code:
    $textarray=spliti('\[PAGEBREAK]',$joketext);
    
    I cant find anything on google about PAGEBREAK used in split or spliti..... can someone explain how this variable exactly works? Is it automatically assigned after a page ends or what
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    "[PAGEBREAK]" is just the pattern that split() will look for.
    If $joketext = "aaa[pagebreak]bbb[pagebreak]ccc[pagebreak]ddd" then the result of split() will be:
    textarray[0] = "aaa"
    textarray[1] = "bbb"
    textarray[2] = "ccc"
    textarray[3] = "ddd"
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    So is [PAGEBREAK] part of regular expressiosn and you dont need to put it between aaa and bbb for example
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    [PAGEBREAK] is an arbitrary string used for the purpose of the tutorail. It has nothing to do with regular expressions.
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    then i am even more confused because there is a php file that contains that line
    Code:
    <!-- joke.php -->
    <html>
    <head>
    <title> Joke </title>
    </head>
    <body>
    <?php
    
    $dbcnx = mysql_connect('localhost', 'root', 'mypasswd');
    mysql_select_db('jokes');
    
    // 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("\n\n",'</p><p>',$joketext);
    $joketext = ereg_replace("\n",'<br />',$joketext);
    
    // Hyperlinks
    $joketext = ereg_replace(
      '\[L]([-_./a-zA-Z0-9!&%#?+,\'=:~]+)\[EL]',
      '<a href="\\1">\\1</a>', $joketext);
    $joketext = ereg_replace(
      '\[L=([-_./a-zA-Z0-9!&%#?+,\'=:~]+)]'.
      '([-_./a-zA-Z0-9 !&%#?+$,\'"=:;~]+)\[EL]',
      '<a href="\\1">\\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>');
    }
    
    ?>
    <p><a href="index.php">Back to the front page</a></p>
    </body>
    </html>
    
    However no where else (in other php files where they create database) [PAGEBREAK] is used... so how the hell would it split up the pages?
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The [PAGEBREAK] symbol is specific to whatever format is used to store jokes in their database. It's not something specific to php or regular expressions. Apparently, the string [PAGEBREAK] will appear in whatever text that comes out of the database to denote a page boundary. The string [PAGEBREAK] is given as the 1st argument to spliti(), so the spliti() function will just seek out occurences of that string in $joketext and split it along those boundaries. spliti() is merely a substring search that uses regular expressions. There are no specific keywords defined for it.
     

Share This Page