SharkBait Posted July 6, 2006 Share Posted July 6, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/ Share on other sites More sharing options...
wildteen88 Posted July 6, 2006 Share Posted July 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54047 Share on other sites More sharing options...
SharkBait Posted July 6, 2006 Author Share Posted July 6, 2006 [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! Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54050 Share on other sites More sharing options...
SharkBait Posted July 6, 2006 Author Share Posted July 6, 2006 [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? Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54052 Share on other sites More sharing options...
wildteen88 Posted July 6, 2006 Share Posted July 6, 2006 What do you mean by one instace? It should parse the bbcodes stated multiple times Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54072 Share on other sites More sharing options...
SharkBait Posted July 6, 2006 Author Share Posted July 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54084 Share on other sites More sharing options...
SharkBait Posted July 6, 2006 Author Share Posted July 6, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54151 Share on other sites More sharing options...
wildteen88 Posted July 7, 2006 Share Posted July 7, 2006 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54306 Share on other sites More sharing options...
Daniel0 Posted July 7, 2006 Share Posted July 7, 2006 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]. Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54309 Share on other sites More sharing options...
wildteen88 Posted July 7, 2006 Share Posted July 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54331 Share on other sites More sharing options...
Daniel0 Posted July 7, 2006 Share Posted July 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54332 Share on other sites More sharing options...
SharkBait Posted July 7, 2006 Author Share Posted July 7, 2006 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] :) Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54402 Share on other sites More sharing options...
wildteen88 Posted July 7, 2006 Share Posted July 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13875-bbcode-howto/#findComment-54404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.