Dysan Posted March 31, 2008 Share Posted March 31, 2008 Hi, I have a textbox within a form, that contains the following: [ b ]This is bold text[ /b ] Upon submitting the form, how do I convert the [ b ] tags into <b></b> tags, to display all the text bold? - Like below? This is bold text Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/ Share on other sites More sharing options...
paul2463 Posted March 31, 2008 Share Posted March 31, 2008 try something like this <?php $array1 = array("[","]"); $array2 = array("<",">"); $string = "[b] Hello World [/b]"; echo "$string <BR>"; echo str_replace($array1,$array2,$string); ?> prints out Hello World <b>Hello World</b> Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505704 Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 Here is some code that converts many different BBCode tags. Take out whatever you don't want to use: <?php //bbcode $patterns = array( //BB Code "/\[([bisu]|marquee)\](.*?)\[\/\\1\]/i", "/\[url\](.*?)\[\/url\]/i", "/\[url=(.*?)\](.*?)\[\/url\]/i", "/\[img\](.*?)\[\/img\]/i", "/\[quote\](.*?)\[\/quote\]/i", "/\[code\](.*?)\[\/code\]/i", "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/i", "/\[marquee\](.*?)\[\/marquee\]/i", "/\[br\]/i", //Emoticons "/\:\)/", "/\:\(/", "/\:O/", "/\:P/", "/\:\|/", "/\:D/", "/\:\?/", "/\;\)/"); $replacements = array( //BB Code "<\\1>\\2</\\1>", "<a href=\"\\1\">\\1</a>", "<a href=\"\\1\" target=\"_blank\">\\2</a>", "<img border=\"0\" src=\"\\1\">", "<div><b>Quote:</b> <i>\\1</i></div>", "<b>Code:</b><div style=\"line-height:12px; width:99%; white-space:nowrap; overflow:auto; max-height:25em;\">\\1</div>", "<font \\1=\"\\2\">\\3</font>", "<br />", //Emoticons "<img src=\"smilies/happy.gif\" border=\"0\">", "<img src=\"smilies/angry.gif\" border=\"0\">", "<img src=\"smilies/omg.gif\" border=\"0\">", "<img src=\"smilies/tounge.gif\" border=\"0\">", "<img src=\"smilies/dry.gif\" border=\"0\">", "<img src=\"smilies/biggrin.gif\" border=\"0\">", "<img src=\"smilies/confused.gif\" border=\"0\">", "<img src=\"smilies/wink.gif\" border=\"0\">" ); $string = "this string has [b]bold text[/b] in it"; $result = preg_replace($patterns,$replacements,$string); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505705 Share on other sites More sharing options...
Dysan Posted March 31, 2008 Author Share Posted March 31, 2008 try something like this <?php $array1 = array("[","]"); $array2 = array("<",">"); $string = "[b] Hello World [/b]"; echo "$string <BR>"; echo str_replace($array1,$array2,$string); ?> prints out Hello World <b>Hello World</b> Using paul2463's method, how do I insert an image? Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505723 Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 Using paul2463's method, how do I insert an image? What image? You asked how to transform BBCode bold tags into HTML Bold tags - so that is what paul2436 provided. I made the assumption that you may need more than just the ability to handle bold tags, so I gave you a comprehensive solution - that also includes images. However, I made an assumption. If you need more than bold functionality you need to state that. Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505800 Share on other sites More sharing options...
Dysan Posted March 31, 2008 Author Share Posted March 31, 2008 OK Sorry. How do I also create: Lists Hyperlinks Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505858 Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 OK Sorry. How do I also create: Lists Hyperlinks Did you look at the code I provided? It already has support for links. As for Lists, how do you want it to work? Do you want the user to specify each list item or do you want to interpret a link break as a new list item? Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505872 Share on other sites More sharing options...
Dysan Posted March 31, 2008 Author Share Posted March 31, 2008 I have looked, and have come up with the following code. With reference to the list, how do I allow the user to specify each list item? Also, using my method below, how do I add hyper links? <?php $tags = array("&" => "&", "<" => "<", ">" => ">", "[b]" => "<b>", "[/b]" => "</b>", "[i]" => "<i>", "[/i]" => "</i>", "[u]" => "<u>", "[/u]" => "</u>", "[img]" => "<img src='", "[/img]" => "'>"), "[url]" => "<a href='", "[/url]" => "'>"); $parsedtext = str_replace(array_keys($tags), array_values($tags), $text); echo $parsedtext; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505915 Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 I have looked, and have come up with the following code. With reference to the list, how do I allow the user to specify each list item? Also, using my method below, how do I add hyper links? You can't add hyperlinks to that code - you need to use preg_replace with the appropriate regualr expressions - which I already provided. This doesn't need to be difficult, just use the code I provided. I have added comments to the code so you can see what is supported. <?php //bbcode $patterns = array( //BB Code "/\[([bisu]|marquee)\](.*?)\[\/\\1\]/i", //Handles bold, italic, strikethrough & underline "/\[url\](.*?)\[\/url\]/i", //Handles URLs w/o link description "/\[url=(.*?)\](.*?)\[\/url\]/i", //Handles URLs w/ link description "/\[img\](.*?)\[\/img\]/i", //handles images "/\[quote\](.*?)\[\/quote\]/i", //handles quote blocks "/\[code\](.*?)\[\/code\]/i", //Handles code blocks "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/i", //Handles font sizing "/\[br\]/i", //Handles line breaks $replacements = array( //BB Code "<\\1>\\2</\\1>", //Handles bold, italic, strikethrough & underline "<a href=\"\\1\">\\1</a>", //Handles URLs w/o link description "<a href=\"\\1\" target=\"_blank\">\\2</a>", //Handles URLs w/ link description "<img border=\"0\" src=\"\\1\">", //handles images "<div><b>Quote:</b> <i>\\1</i></div>", //handles quote blocks "<b>Code:</b><div style=\"line-height:12px; width:99%; white-space:nowrap; overflow:auto; max-height:25em;\">\\1</div>", //Handles code blocks "<font \\1=\"\\2\">\\3</font>", //Handles font sizing "<br />", //Handles line breaks ); $string = "this string has [b]bold text[/b] in it"; $result = preg_replace($patterns,$replacements,$string); echo $result; ?> For the lists you need to show how you want the user to enter the tags. Like this [list]Item 1 Item 2 Item 3[/list] Or this [list] [*]Item 1 [*]Item 2 [*]Item 3[/list] Or what??? Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-505928 Share on other sites More sharing options...
Dysan Posted March 31, 2008 Author Share Posted March 31, 2008 Can then user enter the tag like the following? [*]List Item 1 [*]List Item 2 [*]List Item 3 [list Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-506008 Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 Hmm, your code was processed by the foum - include it in [ c o d e ] tags. Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-506021 Share on other sites More sharing options...
Dysan Posted March 31, 2008 Author Share Posted March 31, 2008 Sorry, forgot that Can then user enter the tag like the following? [list] [*]List Item 1 [*]List Item 2 [*]List Item 3 [/list] Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-506025 Share on other sites More sharing options...
discomatt Posted March 31, 2008 Share Posted March 31, 2008 Why not just use http://php.net/bbcode Quote Link to comment https://forums.phpfreaks.com/topic/98831-convert-bb-to/#findComment-506029 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.