Jump to content

bbcode type parsing


RShadow

Recommended Posts

I'm attempting to build a parser similar to bbcode style, and I'm not exactly sure how to approach this... all the methods I can think up in my head would require basicly a character by character analayis that would be .. well pretty complex.. I'm sure there is a better way in PHP. Doing something like smilies is realativly simple.. pretty much a simple search and replace.. however I'm not exactly sure how I would go about searching for a match pair such as [ some tag ] [ /some end tag ]
Link to comment
Share on other sites

Heres a simple BBCode parser:
[code]<?php

function bbcode($txt)
{
    // bbcodes
    $bbcodes = array( "|\[b\](.+)\[/b\]|is",
                      "|\[u\](.+)\[/u\]|is",
                      "|\[i\](.+)\[/i\]|is"
                    );

    // html
    $replace = array( "<strong>$1</strong>",
                      "<u>$1</u>",
                      "<em>$1</em>"
                    );

    $txt = preg_replace($bbcodes, $replace, $txt);

    return nl2br($txt);

}

$str = "[b]hey[/b] a [u][i]BBCode parser[/i][/u]! ";

$str = bbcode($str);

echo $str;

?>[/code]
converts any text surrounded in b, i and u tags into bold, italics and underline text.

For a emoticon parser, [url=http://ryanslife.net/2006/07/12/php-simple-emoticon-support/]heres a tutorial[/url]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.