Jump to content

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54047
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!
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54050
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?
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54052
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.
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54084
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]
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54151
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]
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54306
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].
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54309
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]

:)
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54402
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.
Link to comment
https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54404
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.