Jump to content

Archived

This topic is now archived and is closed to further replies.

RShadow

bbcode type parsing

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 ]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.