• Advertisement
Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    422733

Whee

Sign in to follow this  

148 views

Yay, my Game Programming Gems 5 book arrived today. I'm happy.

I've got my site in a fairly reasonable state now. I'm parsing the BBC Technology, Slashdot or GameDev.Net RSS feeds (randomly selected) for a bit more substance on the page, I've got a very simple thumbnailing script for my Image Of The Day gallery, and I can read posts from my forum. The hardest part of it all was parsing the damn bbcode. Since I've never used regex before, I really didn't have a clue what I was doing. But I finally got it working, with help from Gyzmo, Pouya, and johnb. I can parse: url (both blah and blah.com), img, b, i, u, list (and
    ,
      ,
    1. ), quote, and code.
      The code is a bit... funky, but it works well enough for me. Here's what I have:

      $search = array(
      '/(\[url=)(.+)(\])(.+)(\[\/url\])/',
      '/(\[url\])(.+)(\[\/url\])/',
      '/(\[img:[0-9a-fA-F]{10}\])(.+)(\[\/img:[0-9a-fA-F]{10}\])/',
      '/(\[[Bb]:[0-9a-fA-F]{10}\])(.+)(\[\/[Bb]:[0-9a-fA-F]{10}\])/',
      '/(\[[Ii]:[0-9a-fA-F]{10}\])(.+)(\[\/[Ii]:[0-9a-fA-F]{10}\])/',
      '/(\[[Uu]:[0-9a-fA-F]{10}\])(.+)(\[\/[Uu]:[0-9a-fA-F]{10}\])/',
      '/(\[list:[0-9a-fA-F]{10}\])/',
      '/(\[list=1:[0-9a-fA-F]{10}\])/',
      '/(\[\/list:u:[0-9a-fA-F]{10}\])/',
      '/(\[\/list:o:[0-9a-fA-F]{10}\])/',
      '/(\[\*:[0-9a-fA-F]{10}\])(.+?)/',
      '/(\[quote:[0-9a-fA-F]{10}=\"(.+)\"\])(.+)(\[\/quote:[0-9a-fA-F]{10}\])/',
      '/(\[quote:[0-9a-fA-F]{10}\])/',
      '/(\[\/quote:[0-9a-fA-F]{10}\])/',
      );
      $replace = array(
      '"\\2">\\4',
      '"\\2">\\2',
      '"\\2" alt="Image"/>',
      '\\2',
      '\\2',
      'class="underline">\\2',
      '
        ',
        '
          ',
          '
      ',
      '
    ',
    '
  • \\2
  • ',
    'class="quotetable">
    class="smalltext">\\2 wrote:
    \\3
    ',
    'class="quotetable">
    class="smalltext">Quote:
    ',
    '
    ',
    );

    $post = preg_replace($search,$replace,$row['post_text']);
    $search = array("
      \r\n","
        \r\n","\r\n");
        $replace = array('
          ','
            ','');
            $post = str_replace($search,$replace,$post);
            $post = preg_replace_callback('/(\[code:1:[0-9a-fA-F]{10}\])(.+)(\[\/code:1:[0-9a-fA-F]{10}\])/s',code_callback,$post);
            $post = str_replace("\r\n","
            \n"
            ,$post);
            $post = str_replace('specialtag_newlines_lol',"\n",$post);


First, I replace all the common tags, then I strip newlines from lists. Without that, the page doesn't validate because you end up with

    , and you haven't have
    tags in a list.
    Once that's done, I use a callack to parse tags. They need special handling because I use geshi to parse the code inside them into nicely colured text, much like the source tags here on GDNet.
    Inside the code callback function, I convert special characters back to their normal equivalents (( becomes ( and so on), since phpbb converts those, and geshi will convert them back if it needs. Then whitespace is trimmed, and the code is parsed with geshi. Finally, newlines get converted into a tag, so when I come to convert newlines to br's, I don't end up with
    all over my code.
    Back out of the callback, I convert newlines to
    tags, and then put the newlines back into the code blocks.

    Phew.
    I spent most of yesterday watching Lost, season 1. I started watching it when it was on TV here, but I only saw the first 4 or 5 episodes, then missed about 5 of them and decided it was pointless trying to get back into it, and I'll just get the DVD when it comes out.
    I've still got the second half of the season to watch tonight (Another 9 hours in total). Why is it that when my MSN name is set to "Steve - Watching Lost (Away)", everyone tries to sodding talk to me? I had 5 people leaving messages, including one who kept saying "Pssssst!" "timmy!" and sending repeated nudges. However, my MSN doesn't bleep or anything when people send me messages/nudges when media player is running fullscreen...
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement