Jump to content

Recommended Posts

Hello php experts! I have found the matter of decoding bbcode to be very stumping. I have a simple php encoder that works perfectly, but I can't figure out how to properly decode the information as well.

 

Here is my encoder

 

	function bb_encode ($str) {  
    $str = nl2br(htmlentities($str));  
  
    $simple_search = array(  
                //added line break  
                '/\[br\]/is',  
                '/\[b\](.*?)\[\/b\]/is',  
                '/\[i\](.*?)\[\/i\]/is',  
                '/\[u\](.*?)\[\/u\]/is',  
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
                '/\[url\](.*?)\[\/url\]/is',  
                '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
                '/\[img\](.*?)\[\/img\]/is',  
                '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
                '/\[mail\](.*?)\[\/mail\]/is',  
                '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
                '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
                '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
                  //added textarea for code presentation  
               '/\[codearea\](.*?)\[\/codearea\]/is',  
                 //added pre class for code presentation  
              '/\[code\](.*?)\[\/code\]/is',  
                //added paragraph  
              '/\[p\](.*?)\[\/p\]/is',  
                );  
  
    $simple_replace = array(  
			//added line break  
               '<br />',  
                '<strong>$1</strong>',  
                '<em>$1</em>',  
                '<u>$1</u>',  
			// added nofollow to prevent spam  
                '<a href="$1" rel="nofollow" title="$2 - $1">$2</a>',  
                '<a href="$1" rel="nofollow" title="$1">$1</a>',  
                '<div style="text-align: $1;">$2</div>',  
			//added alt attribute for validation  
                '<img src="$1" alt="" />',  
                '<a href="mailto:$1">$2</a>',  
                '<a href="mailto:$1">$1</a>',  
                '<span style="font-family: $1;">$2</span>',  
                '<span style="font-size: $1;">$2</span>',  
                '<span style="color: $1;">$2</span>',  
			//added textarea for code presentation  
			'<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
			//added pre class for code presentation  
			'<pre class="code">$1</pre>',  
			//added paragraph  
			'<p>$1</p>',  
                );  
  
    // Do simple BBCode's  
    $str = preg_replace ($simple_search, $simple_replace, $str);  
  
    return $str;  
}

 

How would I decode the information? Could someone please show me and example ( a simple one would could be finding <strong>...</strong> tags and replacing them with [  b  ]...[  /b  ] tags )

 

Any help would be greatly appreciated! Thank you!

 

- Pianoman993

Link to comment
https://forums.phpfreaks.com/topic/144295-solved-decode-bbcode/
Share on other sites

I was able to fix the problem.

 

function bb_decode ($str) {  
    $str = ($str);

$bbcode = array(

	"/\<strong>(.*?)<\/strong>/is" => "[b]$1[/b]",
	"/\<br \/>/is" => ""

);

$str = preg_replace(array_keys($bbcode), array_values($bbcode), $str);
  
    return $str;  
}  

Link to comment
https://forums.phpfreaks.com/topic/144295-solved-decode-bbcode/#findComment-757241
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.