Jump to content

bbcode [code] tag


cheesycarrion

Recommended Posts

I'm trying to make it so that any html or bbcode inside the [ code ] tag isn't parsed. here's what I've got: (it doesn't work. stuff inside the [ code ] tag still gets treated like normal.)

<?php
function parse_bbcode($input)
{
$bb_replace = array(
	'/\[img\](.*?)\[\/img\]/is',
	'/\[b\](.*?)\[\/b\]/is',
	'/\[i\](.*?)\[\/i\]/is',
	'/\[u\](.*?)\[\/u\]/is',
	'/\[url\=(.*?)\](.*?)\[\/url\]/is',
	'/\[url_blank\=(.*?)\](.*?)\[\/url_blank\]/is',
	'/\[textarea\](.*?)\[\/textarea\]/is',
	'/\[url\](.*?)\[\/url\]/is',
	'/\[url_blank\](.*?)\[\/url_blank\]/is',
	'/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
	'/\[quote\](.*?)\[\/quote\]/is',
	'/\[code\](.*?)\[\/code\]/is',
	'/\[color\=(.*?)\](.*?)\[\/color\]/is',
	'/\[size\=(.*?)\](.*?)\[\/size\]/is'
	);
$bb_replacements = array(
	'<img src="$1" alt="$1" />',
	'<span style="font-weight: bold;">$1</span>',
	'<span style="font-style: italic;">$1</span>',
	'<span style="text-decoration: underline;">$1</span>',
	'<a href="$1">$2</a>',
	'<a href="$1" target="_blank">$2</a>',
	'<textarea>$1</textarea>',
	'<a href="$1">$1</a>',
	'<a href="$1" target="_blank">$1</a>',
	'<span class="quote">$1 said:<span>$2</span></span>',
	'<span class="quote">quote:<span>$1</span></span>',
	'<code>' . str_replace(array("[","]"),array("&#91","&#93"),htmlspecialchars('$1')) . '</code>',
	'<span style="color:$1;">$2</span>',
	'<span style="font-size:$1;">$2</span>'
	);
return preg_replace($bb_replace,$bb_replacements,$input);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/137752-bbcode-code-tag/
Share on other sites

try this:

<?php
function parse_bbcode($input)
   {

preg_match_all('/\[code\](.*?)\[\/code\]/is',$input, $code)

foreach($code[0] as $cur) {

$input = str_replace($cur[0],htmlspecialchars($cur[0]),$input);

}

   $bb_replace = array(
      '/\[img\](.*?)\[\/img\]/is',
      '/\[b\](.*?)\[\/b\]/is',
      '/\[i\](.*?)\[\/i\]/is',
      '/\[u\](.*?)\[\/u\]/is',
      '/\[url\=(.*?)\](.*?)\[\/url\]/is',
      '/\[url_blank\=(.*?)\](.*?)\[\/url_blank\]/is',
      '/\[textarea\](.*?)\[\/textarea\]/is',
      '/\[url\](.*?)\[\/url\]/is',
      '/\[url_blank\](.*?)\[\/url_blank\]/is',
      '/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
      '/\[quote\](.*?)\[\/quote\]/is',
      '/\[code\](.*?)\[\/code\]/is',
      '/\[color\=(.*?)\](.*?)\[\/color\]/is',
      '/\[size\=(.*?)\](.*?)\[\/size\]/is'
      );
   $bb_replacements = array(
      '<img src="$1" alt="$1" />',
      '<span style="font-weight: bold;">$1</span>',
      '<span style="font-style: italic;">$1</span>',
      '<span style="text-decoration: underline;">$1</span>',
      '<a href="$1">$2</a>',
      '<a href="$1" target="_blank">$2</a>',
      '<textarea>$1</textarea>',
      '<a href="$1">$1</a>',
      '<a href="$1" target="_blank">$1</a>',
      '<span class="quote">$1 said:<span>$2</span></span>',
      '<span class="quote">quote:<span>$1</span></span>',
      '<code>$1</code>',
      '<span style="color:$1;">$2</span>',
      '<span style="font-size:$1;">$2</span>'
      );
   return preg_replace($bb_replace,$bb_replacements,$input);
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/137752-bbcode-code-tag/#findComment-720184
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.