blueman378 Posted March 20, 2008 Share Posted March 20, 2008 hi guys, well heres the code im using, <?php //bbcode $patterns = array( "/\[url\](.*?)\[\/url\]/", "/\[img\](.*?)\[\/img\]/", "/\[b\](.*?)\[\/B\]/", "/\[b\](.*?)\[\/b\]/", "/\[u\](.*?)\[\/U\]/", "/\[u\](.*?)\[\/u\]/", "/\[i\](.*?)\[\/I\]/", "/\[i\](.*?)\[\/i\]/", "/\[quote\](.*?)\[\/quote\]/", "/\[quote\](.*?)\[\/QUOTE\]/", "/\[code\](.*?)\[\/code\]/", "/\[code\](.*?)\[\/CODE\]/", "/\[s\](.*?)\[\/s\]/", "/\[s\](.*?)\[\/S\]/", "/\[url=(.*?)\](.*?)\[\/url\]/", "/\[color=(.*?)\](.*?)\[\/color\]/", "/\[size=(.*?)\](.*?)\[\/size\]/", "/\[marquee\](.*?)\[\/marquee\]/", "/\[br\]/", //emoticons "/\:\)/", "/\:\(/", "/\:O/", "/\:P/", "/\:\|/", "/\:D/", "/\:\?/",); $replacements = array( //bbcode "<a href=\"\\1\">\\1</a>", "<img border=0 src='\\1'>", "<b>\\1</b>", "<b>\\1</b>", "<u>\\1</u>", "<u>\\1</u>", "<i>\\1</i>", "<i>\\1</i>", "<div><b>Quote:</b> <i>\\1</i></div>", "<div><b>Quote:</b> \\1</div>", "<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>", "<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>", "<s>\\1</s>", "<s>\\1</s>", "<a href=\"\\1\" target=\"_blank\">\\2</a>", "<font color=\"\\1\">\\2</font>", "<font size=\"\\1\">\\2</font>", "<marquee>\\1</marquee>", "<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\">" ); ?> which works perfect, i am wanting to add a smiley in however whenever i try it reads it as the end of the array and spits out an error, how would i stop this from doing that? i tried escaping it with a \ but it doesnt seem to be working Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 20, 2008 Share Posted March 20, 2008 first make sure there is not a comma before then ending bracket like so: ,); will probably call an error. i dont see why its not working i shall test on my system. TESTED. <?php //bbcode $patterns = array( "/\[url\](.*?)\[\/url\]/", "/\[img\](.*?)\[\/img\]/", "/\[b\](.*?)\[\/B\]/", "/\[b\](.*?)\[\/b\]/", "/\[u\](.*?)\[\/U\]/", "/\[u\](.*?)\[\/u\]/", "/\[i\](.*?)\[\/I\]/", "/\[i\](.*?)\[\/i\]/", "/\[quote\](.*?)\[\/quote\]/", "/\[quote\](.*?)\[\/QUOTE\]/", "/\[code\](.*?)\[\/code\]/", "/\[code\](.*?)\[\/CODE\]/", "/\[s\](.*?)\[\/s\]/", "/\[s\](.*?)\[\/S\]/", "/\[url=(.*?)\](.*?)\[\/url\]/", "/\[color=(.*?)\](.*?)\[\/color\]/", "/\[size=(.*?)\](.*?)\[\/size\]/", "/\[marquee\](.*?)\[\/marquee\]/", "/\[br\]/", //emoticons "/\:\)/", "/\:\(/", "/\:O/", "/\:P/", "/\:\|/", "/\:D/", "/\:\?/", "/\;\)/"); $replacements = array( //bbcode "<a href=\"\\1\">\\1</a>", "<img border=0 src='\\1'>", "<b>\\1</b>", "<b>\\1</b>", "<u>\\1</u>", "<u>\\1</u>", "<i>\\1</i>", "<i>\\1</i>", "<div><b>Quote:</b> <i>\\1</i></div>", "<div><b>Quote:</b> \\1</div>", "<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>", "<b>Code:</b><div style='line-height: 12px; width: 99%; white-space: nowrap; overflow: auto; max-height: 25em;'>\\1</div>", "<s>\\1</s>", "<s>\\1</s>", "<a href=\"\\1\" target=\"_blank\">\\2</a>", "<font color=\"\\1\">\\2</font>", "<font size=\"\\1\">\\2</font>", "<marquee>\\1</marquee>", "<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\">" ); ?> works perfect for me. used this code: <?php $string = "this string has a smiley in it"; $result = preg_replace($patterns,$replacements,$string); print_r($result); ?> to test it Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 20, 2008 Author Share Posted March 20, 2008 yeah its worknig now odd, thanks but could anyone take a quick look at this topic for me? http://www.phpfreaks.com/forums/index.php/topic,188170.msg843578.html Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 20, 2008 Share Posted March 20, 2008 FYI: You can cut your code nearly in half by making the patterns case insensitive: Instead of "/\[b\](.*?)\[\/B\]/", "/\[b\](.*?)\[\/b\]/", Use this, with the "i" parameter to indicate the patwtern should be case insensitive. "/\[b\](.*?)\[\/b\]/i", Quote Link to comment Share on other sites More sharing options...
effigy Posted March 20, 2008 Share Posted March 20, 2008 ...and grouping: /\[([bisu])\](.*?)\[\/\1\]/i Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 20, 2008 Author Share Posted March 20, 2008 thanks mjdamato, that shortened it alot, but i dont really get your one effigy? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 20, 2008 Share Posted March 20, 2008 i dont really get your one effigy? It handles four tags in one--the b, i, s, and u. It is captured so \1 can be used to find the matching end tag. Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 20, 2008 Author Share Posted March 20, 2008 ah right that makes sense, one more question, this code works as long as the data is in the same line, eg [ code ] data here [ / code ] (without spaces) works but [ code ] data here [ / code ] (without spaces) doesnt get replaced, any way to get the replace to span over multiple lines? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 20, 2008 Share Posted March 20, 2008 . does not include new lines by default. This can be changed with the /s modifier: /pattern/s Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 20, 2008 Author Share Posted March 20, 2008 sorry dude, how would i apply the modifier? its kinda late/early here so im tired but need to get this done heres the thing im using, $q = preg_replace($patterns,$replacements, $q); Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 20, 2008 Author Share Posted March 20, 2008 ok never mind that was a stupid question... 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.