Jump to content


Photo

BBCode - HowTo?


  • Please log in to reply
12 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 06 July 2006 - 06:20 PM

Hi,

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

What do I need to use to change

abkasdfas


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?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 July 2006 - 06:33 PM

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:
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);


#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 06 July 2006 - 06:53 PM

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:

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);


Ahh that will be a good start. Thanks!

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 06 July 2006 - 06:58 PM

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:


Ahh that will be a good start. Thanks!


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?

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 July 2006 - 07:29 PM

What do you mean by one instace? It should parse the bbcodes stated multiple times

#6 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 06 July 2006 - 07:43 PM

It did the out most tags. The inner ones showed up as their tags.


see if I an recreate it here

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


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.

#7 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 06 July 2006 - 10:25 PM

This is what I am getting with the following:

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

Posted Image


#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 July 2006 - 09:50 AM

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:
<?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);

?>


#9 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 07 July 2006 - 10:07 AM

Hey,

You might want to check out the PEAR package called HTML_BBCode_Parser. It's a package already written for people who would like to use it and it comes with a documentation.

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 July 2006 - 11:39 AM

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.

#11 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 07 July 2006 - 11:43 AM

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.

#12 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 07 July 2006 - 02:42 PM

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



:)

#13 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 July 2006 - 02:45 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users