lobsterninja Posted March 9, 2007 Share Posted March 9, 2007 So I posted on codigforums.com, but unfortunately no one replied, so I'm going to try my luck here. Hello. I've been having a bit of trouble with some code that I've been writing, which deals with regular expressions, and more specifically with translating BB Code to HTML code and vice versa. The first bot of code takes a normal bit of text (from whatever the user posted) and runs it through a preg_replace, changing BB Code to HTML. The second bit does the opposite: it takes some text and changes all the HTML to BB Code (so the user can see the BB Code when he edits the text). For some reason, the first bit isn't working as I expected, pretty much only with the url img and quote tags. The second bit works as far as I know. On a side note, how would I change breaks in what a user has typed (in a textarea) into <br>'s with regular exprecssions? I tried using /n (I just now realize it's \n), but that didn't seem to work, or at least I couldn't get it to. Anyway, please excuse any really stupid sounding grammar, it's about 4am and I've been having a bit of trouble concentrating. Thanks in advance! Bit of code 1: <?php $input = array( "/\[b\]/", "/\[\/b\]/", "/\[i\]/", "/\[\/i\]/", "/\[u\]/", "/\[\/u\]/", "/\[url=(.*?)\]/", "/\[\/url\]/", "/\[img\](.*?)\[\/img\]/", "/\[quote\](.*?)\[\/quote\]/" ); $code = array( "<b>", "</b>", "<i>", "</i>", "<u>", "</u>", "<a href='\${1}'>", "</a>", "<img src='\${1}'>", "<div class='quote-box-right'>\${1}</div>" ); $message = preg_replace($input,$code,$message); ?> Bit of code 2: <?php $code = array( "[b]", "[/b]", "[i]", "[/i]", "[u]", "[/u]", "[url=\${1}]", "[/url]", "[img=\${1}]", "[quote]\${1}[/quote]" ); $input = array( "/\<b\>/", "/\<\/b\>/", "/\<i\>/", "/\<\/i\>/", "/\<u\>/", "/\<\/u\>/", "/\<a href=\"(.*?)\"\>/", "/\<\/a\>/", "/\<img src=\"(.*?)\"\>/", "/\<div class=\"quote-box-right\"\>(.*?)\<\/div\>/" ); $message = preg_replace($input,$code,$message); ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 9, 2007 Share Posted March 9, 2007 Whn converting BBCode to HTML it is best to it when you go to show it to the user in your webpage rather than convert it to HTML first and then save it in the database. Then having to go through the trouble of converting the HTML to BBCode when you want to edit what was submitted. You should keep what ever is submitted in its raw form. When converting using regex dont do this: $input = array( "/\[b\]/", "/\[\/b\]/" ); Use the full of power of regex and do this: $input = array("/\[b\](.*?)\[\/b\]/is" ); That is much more better rather converting each closing/opening tag separately. The trick here is to use pattern modifiers notice the i and s after the closing the delimiter (/) those tell the PCRE engine to ignore newlines (allowing for the parsing to span multiple lines rather than one) and to be case insensitive so is the same as . Quote Link to comment Share on other sites More sharing options...
lobsterninja Posted March 9, 2007 Author Share Posted March 9, 2007 Ok, so I should use regex to change the BB Code or whatever I stored in the database to HTML when I display it on the webpage? Hopefully once I get this working it will solve my problem. Thanks! 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.