Jump to content

BB Code to HTML code problem


lobsterninja

Recommended Posts

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);
?> 

Link to comment
Share on other sites

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 .

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.