Jump to content

Archived

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

SharkBait

BBCode - HowTo?

Recommended Posts

Hi,

  I'm sure this is in numerous posts, but I cant seem to find it.

What do I need to use to change [quote]abkasdfas[/quote]

To display <div class="quote">abkasdfas</div> back to the users?  Like the way forums do it :)

Do I need to look at preg_replace() type thing?

Share this post


Link to post
Share on other sites
Yes you'll want to use preg_replace in order to find exactly what you want within a string. Heres is a simple BBCode parser:
[code]function doBBCode($str)
{
    $code = array("#\[b\](.*?)\[/b\]#is",
                  "#\[i\](.*?)\[/i\]#is",
                  "#\[u\](.*?)\[/u\]#is"
                  );

    $html = array("<b>\\1</b>",
                  "<i>\\1</i>",
                  "<u>\\1</u>"
                  );

    $str = preg_replace($code, $html, $str);

    return $str;
}

$text = "[b]Hello![/b], [i]How [u]are[/u] you?[/i], [b][i][u]I'm fine![/u][/i][/b]";

echo doBBCode($text);[/code]

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=99692.msg392721#msg392721 date=1152210802]
Yes you'll want to use preg_replace in order to find exactly what you want within a string. Heres is a simple BBCode parser:
[code]function doBBCode($str)
{
    $code = array("#\[b\](.*?)\[/b\]#is",
                  "#\[i\](.*?)\[/i\]#is",
                  "#\[u\](.*?)\[/u\]#is"
                  );

    $html = array("<b>\\1</b>",
                  "<i>\\1</i>",
                  "<u>\\1</u>"
                  );

    $str = preg_replace($code, $html, $str);

    return $str;
}

$text = "[b]Hello![/b], [i]How [u]are[/u] you?[/i], [b][i][u]I'm fine![/u][/i][/b]";

echo doBBCode($text);[/code]
[/quote]

Ahh that will be a good start. Thanks!

Share this post


Link to post
Share on other sites
[quote author=SharkBait link=topic=99692.msg392724#msg392724 date=1152212034]
[quote author=wildteen88 link=topic=99692.msg392721#msg392721 date=1152210802]
Yes you'll want to use preg_replace in order to find exactly what you want within a string. Heres is a simple BBCode parser:

[/quote]

Ahh that will be a good start. Thanks!
[/quote]

Ok now I'm just testing double quotes.

**edit:  I noticed with the code you replied with, it doesnt want to replace more than 1 instance.  Do I have to loop through it or?

Share this post


Link to post
Share on other sites
What do you mean by one instace? It should parse the bbcodes stated multiple times

Share this post


Link to post
Share on other sites
It did the out most tags. The inner ones showed up as their tags.


see if I an recreate it here

[quote] \quote] this is a quote and \quote]]my reply here [/quote]

Now I had to take out the right [ on the inner quote but thats how it somewhat looks like.

Perhaps I am missing something. I'll look through it again.

Share this post


Link to post
Share on other sites
This is what I am getting with the following:

[code=php:0]
function doTyCode($str) {
$code = array("#\[quote\](.*?)\[/quote\]#is",
);
$html = array("<div class=\"quote\">\\1</div>",
);
$str = preg_replace($code, $html, $str);

return $str;
}
[/code]

[img]http://64.251.82.21/~tingram/quote.gif[/img]

Share this post


Link to post
Share on other sites
Argh. I see now. Okay if you are doing quotes you'll have to use a function called preg_match which will return an array of all the matches for your quote tags. The you'll use a while loop to loop through all the matches and then use preg_replace to convert the bbcode. Heres an example:
[code]<?php

function doTyCode($str)
{
    $code = array("#\[b\](.*?)\[/b\]#is",
                  "#\[i\](.*?)\[/i\]#is",
                  "#\[u\](.*?)\[/u\]#is"
                  );

    $html = array("<b>\\1</b>",
                  "<i>\\1</i>",
                  "<u>\\1</u>"
                  );

    $str = preg_replace($code, $html, $str);

    // call dedicated function for replacing quotes
    $str = doTyQuotes($str);

    return $str;
}

function doTyQuotes($str)
{
    $code = "#\[quote\](.*?)\[/quote\]#is";
    $html = "<div class=\"quote\">\\1</div>";

    // when a match is found, replace the match
    while(preg_match($code, $str))
    {
        $str = preg_replace($code, $html, $str);
    }

    return $str;
}

$txt = "[quote][quote]Hi! [b]How[/b] are you[/quote] Yeah I'm fine![/quote]";

echo doTyCode($txt);

?>[/code]

Share this post


Link to post
Share on other sites
Hey,

You might want to check out the [url=http://pear.php.net]PEAR[/url] package called [url=http://pear.php.net/package/HTML_BBCodeParser]HTML_BBCode_Parser[/url]. It's a package already written for people who would like to use it and it comes with a [url=http://pear.php.net/package/HTML_BBCodeParser/docs]documentation[/url].

Share this post


Link to post
Share on other sites
Its always better to create your own scripts rather than using something someone else has made for you. That way you get the knowledge of how create a BBCode parser plus you can then customise your own script your own needs.

Share this post


Link to post
Share on other sites
Sure it is, but sometimes people may not be able to code a feature/function themself. But looking at other people's code is a great way to improve your own coding skills if you are not very good in a certain language, I think.

Share this post


Link to post
Share on other sites
wildteen88 - Thanks that works nicely and I see how it has to loop through.  I need to brush up on my regex ;) 

Daniel0 - I saw that PEAR module a while back, but thought I would see how it is done without PEAR.  Thanks though.

I did a reverse of the doTyCode() so that when a user replies to the message, it converts the <div></div> back into [quote][/quote]

:)

Share this post


Link to post
Share on other sites
SharkBait are you parsing the BBCode when the message is submitted to the database? If you are it will be better it was parsed when you extract the message from the database. That way when you go to edit the code at later date it still in its raw BBCode state, saving having to create a DeBBCode function.

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.