gordsmash Posted September 30, 2009 Share Posted September 30, 2009 hello, i have searched this for a while with no results. But is there a way to program your own bb codes with php or is this a dumb question. Any answers are greatly appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted September 30, 2009 Share Posted September 30, 2009 Yes there is. Quote Link to comment Share on other sites More sharing options...
gordsmash Posted September 30, 2009 Author Share Posted September 30, 2009 Are there any example codes that you have? Quote Link to comment Share on other sites More sharing options...
trq Posted September 30, 2009 Share Posted September 30, 2009 I myself don't have any examples but I'm sure Googling 'php bbcode' would get you some results. Never seen the point in bbcode myself. Quote Link to comment Share on other sites More sharing options...
gordsmash Posted September 30, 2009 Author Share Posted September 30, 2009 Is it possible to use the str_replace function to code them that way? Or is the PEAR way the only way. Quote Link to comment Share on other sites More sharing options...
Alex Posted September 30, 2009 Share Posted September 30, 2009 Sure, you can use str_replace, or even better would be preg_replace() Quote Link to comment Share on other sites More sharing options...
Evilace Posted September 30, 2009 Share Posted September 30, 2009 try these, i made one from this google phpbb or http://www.phpbb.com/ Quote Link to comment Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 Why would preg_replace be better? If gordsmash just wishes to substitute one string for the other surely str_replace would be better. Quote Link to comment Share on other sites More sharing options...
Alex Posted September 30, 2009 Share Posted September 30, 2009 Why would preg_replace be better? If gordsmash just wishes to substitute one string for the other surely str_replace would be better. If you were doing str_replace you'd do something like this: $text = str_replace(Array('[b]', '[/b]'), Array('<b>', '</b>'), $text); Now say if someone entered this text: [b]Hello world There would be no closing </b> tag, which can cause problems (be it display problems or validation issues). Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2009 Share Posted September 30, 2009 Here is some sample code I have laying around. You can add your own options by modifying $patters and $replacements accordingly <?php //bbcode $patterns = array( //BB Code "/\[[b]\](.*?)\[\/b\]/is", "/\[[i]\](.*?)\[\/i\]/is", "/\[[u]\](.*?)\[\/u\]/is", "/\[[s]\](.*?)\[\/s\]/is", "/\[marquee\](.*?)\[\/marquee\]/is", "/\[url\](.*?)\[\/url\]/is", "/\[url=(.*?)\](.*?)\[\/url\]/is", "/\[img\](.*?)\[\/img\]/is", "/\[quote\](.*?)\[\/quote\]/is", "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/is", "/\[br\]/i", //Emoticons "/ \:\) /", "/ \:\( /", "/ \ /", "/ \ /", "/ \:\| /", "/ \ /", "/ \:\? /", "/ \;\) /"); $replacements = array( //BB Code "<b>\\1</b>", "<i>\\1</i>", "<u>\\1</u>", "<s>\\1</s>", "<marquee>\\1</marquee>", "<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>", "<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 = "[b]This is bold text[/b] [i]this is italic text[/i] [b][i]This is bold, italic text[/b][/i]"; $result = preg_replace($patterns,$replacements,$string); echo $result; ?> EDIT: had to remove the BBCode optins for [ code ] as it was screwing up my post. Quote Link to comment Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 Ahh yes, point taken. Quote Link to comment Share on other sites More sharing options...
gordsmash Posted September 30, 2009 Author Share Posted September 30, 2009 sweet! thanks everyone for helping me out. You guys are awesome!!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2009 Share Posted September 30, 2009 Why would preg_replace be better? If gordsmash just wishes to substitute one string for the other surely str_replace would be better. If you were doing str_replace you'd do something like this: $text = str_replace(Array('[b]', '[/b]'), Array('<b>', '</b>'), $text); Now say if someone entered this text: [b]Hello world There would be no closing </b> tag, which can cause problems (be it display problems or validation issues). Not only that, there is no way you could use str_replace() to parse something like "Go here" into a valid link without regular expressions. Well, I suppose you could do it with a lot of various scripts to process the text multiple times, but it would be pointless since you can do it very easily with regular expressions. Quote Link to comment 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.