pandailo Posted September 13, 2008 Share Posted September 13, 2008 Hi all, Actually i manage to create bbcode function but there is a problem especially with user accidentally mistype the bbcode For example, if user type the bbcode like this test, it will give an parse error. How do i solve the issue Here is my bbcode function function getbbcode($text, $ses="", $filtered) { $text = htmlspecialchars($text); $text=preg_replace("/\[b\](.*?)\[\/b\]/i","<b>\\1</b>", $text); $text=preg_replace("/\[b\](.*?)\[\/u\]/i","<b>\\1</b>", $text); $text=preg_replace("/\[i\](.*?)\[\/i\]/i","<i>\\1</i>", $text); $text=preg_replace("/\[u\](.*?)\[\/u\]/i","<u>\\1</u>", $text); $text=preg_replace("/\[u\](.*?)\[\/b\]/i","<u>\\1</u>", $text); $text=preg_replace("/\[big\](.*?)\[\/big\]/i","<big>\\1</big>", $text); $text=preg_replace("/\[small\](.*?)\[\/small\]/i","<small>\\1</small>", $text); return $text; } I tried belows way to solve the issue: $text=preg_replace("/\[b\][u\](.*?)\[\/b\][\/u\]/i","<b>\\1</b>", $text); $text=preg_replace("/\[b\][u\](.*?)\[\/b\]\[\/u\]/i","<b>\\1</b>", $text); $text=preg_replace("/\[b\]/\[u\](.*?)\[\/b\]\[\/u\]/i","<b>\\1</b>", $text); but none is working. Anybody can solve the issue? Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/ Share on other sites More sharing options...
Garethp Posted September 13, 2008 Share Posted September 13, 2008 For BBCode, try this instead, it's what I use $Find = array ( '/\[b\]/is', '/\[\/b\]/is', '/\[i\]/is', '/\[\/i\]/is' '/\[u\]/is', '/\[\/u\]/is', '/\[big\]/is', '/\[\/big\]/is', '/\[small\]/is', '/\[\/small\]/is' ); $Replace = array ( '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<big>', '</big>', '<small>', '</small>' ); $Text = preg_replace($Search, $Replace, $Text); Your problem was it replaced [xx]aa[/xx] with <xx>aa</xx>. You didn't say what to do if the [/xx] wasn't matched with anything. My code tells it to replace, no matter what Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-640486 Share on other sites More sharing options...
pandailo Posted September 15, 2008 Author Share Posted September 15, 2008 sorry i have to make you clear on this. Actually the actual problem is like below when user type [xx][yy]testing[/xx][/yy] <-- this is mistype by user and give me parse error. So i do i solve that kind of issue? Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-641785 Share on other sites More sharing options...
Garethp Posted September 15, 2008 Share Posted September 15, 2008 Use my code, again, it will work Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-641822 Share on other sites More sharing options...
pandailo Posted September 16, 2008 Author Share Posted September 16, 2008 your function is working but the result is empty message(no single word appear in message)..and still not solve..any way to improve it? Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-642534 Share on other sites More sharing options...
xoligy Posted September 16, 2008 Share Posted September 16, 2008 im using this one $txt = str_replace("[b]", "<b>", $txt); $txt = str_replace("[/b]", "</b>", $txt); $txt = str_replace("[i]", "<i>", $txt); $txt = str_replace("[/i]", "</i>", $txt); $txt = str_replace("[u]", "<u>", $txt); $txt = str_replace("[/u]", "</u>", $txt); $txt = str_replace("[quote]", "<div id=quote><b>QUOTE:</b>", $txt); $txt = str_replace("[/quote]", "</div>", $txt); $txt = preg_replace("#\[color=(.+?)\](.+?)\[/color\]#is","<font color=\"\\1\">\\2</font>",$txt); Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-642536 Share on other sites More sharing options...
Garethp Posted September 16, 2008 Share Posted September 16, 2008 Nah, I wrote up what you need, here it is <?php function BBCode($Text) { $Find = array ( '/\[b\]/is', '/\[\/b\]/is', '/\[i\]/is', '/\[\/i\]/is', '/\[u\]/is', '/\[\/u\]/is', '/\[big\]/is', '/\[\/big\]/is', '/\[small\]/is', '/\[\/small\]/is' ); $Replace = array ( '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<big>', '</big>', '<small>', '</small>' ); $Text = preg_replace($Find, $Replace, $Text); return $Text; } $Text = "[u][b]Your message here[/u][/b]"; echo BBCode($Text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-642538 Share on other sites More sharing options...
pandailo Posted September 20, 2008 Author Share Posted September 20, 2008 i know what y'all trying to help but all your function is working bbcode..no problem to me also...my above function also 4 working bbcode.....my problem still remain unsolve because what i explain in this topic is mistype bbcode just as show in the example above...and to prove the mistype bbcode is working prefectly is in this forum itself. If i type [b ][u ]test[ /b][ /u] --> see the result test that is what i want, when user mistype the bbcode, the function automatically detect bold and underline bbcode like example above. Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646265 Share on other sites More sharing options...
Minase Posted September 20, 2008 Share Posted September 20, 2008 i will give you what i use right now , should be good enough no problem at those "errors" function bb($bbcode){ $bbcode = htmlentities($bbcode); $searchFor = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is' ); $replaceWith = array( '<b>$1</b>', '<i>$1</i>', '<a href="$1">$2</a>' ); $html = preg_replace($searchFor, $replaceWith, $bbcode); return $html; } //edit now saw -.- why do you use [ b] and [ /b] in different arrays? * no space lol* Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646267 Share on other sites More sharing options...
pandailo Posted September 20, 2008 Author Share Posted September 20, 2008 i use space for example of "test" word result...i know there is no space using bbcode...anyway thank you for the function..i will test it out right away. tq Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646361 Share on other sites More sharing options...
pandailo Posted September 20, 2008 Author Share Posted September 20, 2008 Thank you all for your help. PROBLEM SOLVED USING MINASE FUNCTION...THANK YOU VERY MUCH. Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646366 Share on other sites More sharing options...
pandailo Posted September 20, 2008 Author Share Posted September 20, 2008 Thank you all for your help. PROBLEM SOLVED USING MINASE FUNCTION...THANK YOU VERY MUCH. Damn...still not solve....me headache... Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646371 Share on other sites More sharing options...
pandailo Posted September 20, 2008 Author Share Posted September 20, 2008 how to match [b ]blabla[/u] with bold bbcode <b>blabla</b> Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646378 Share on other sites More sharing options...
wildteen88 Posted September 20, 2008 Share Posted September 20, 2008 this will require much more complex code. It is always best to parse BBCode tags in pairs and never on a singular basis. That way if users mistype a tag or place a tag where it shouldn't be your layout wont screw up. You should look into adding a "Preview" area so the user can check whatever they are about to post looks correctly and apply any amendments before posting their message. Quote Link to comment https://forums.phpfreaks.com/topic/124064-bbcode-function-issue/#findComment-646387 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.