Jump to content

[SOLVED] Implementing a properly functioning [code] tag.


Goldeneye

Recommended Posts

So, this has been on my project To-Do List for a long time. I'm trying to implement a [/code] into my BB-Code function. I use square-bracket tags (such as [/b], [/u], and [/img]). No HTML or JavaScript is parsed, but square-bracket tags are. My goal is to not have the square-bracket tags parsed inside the [/code] tag.

 

<?php
function formatText($str) {
		$str = trim($str);
		$str = stripslashes($str);
		$str = htmlentities($str, ENT_COMPAT, 'UTF-8');
		$str = nl2br($str);
		$search = array('/\[b\](.*?)\[\/b\]/si', '/\[i\](.*?)\[\/i\]/si', '/\[u\](.*?)\[\/u\]/si', '/\[align\=(left|center|right)\](.*?)\[\/align\]/si', '/\[img\](.*?)\[\/img\]/si', '/\%n/si', '/\[priv\](.*?)\[\/priv\]/si', '/\[ascii\](.*?)\[\/ascii\]/si', '/((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)/', '/\[hide\](.*?)\[\/hide\]/si');
		$replace = array('<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<div style="text-align: $1;">$2</div>', '<a href="$1"><img src="$1" class="image"></a>', $_SESSION['pseudonym'], isset($_SESSION['user_id']) ? '$1' : '<p class="sysmessage">For members\' eyes only.</p>', '<span class="ascii">$1</span>', '<a href="$1" rel="nofollow" target="_blank">$1</a>$4', '<span class="view_closed"><span class="view_on_close"><a href="#" onclick="return toggleContent(this)"><strong>&#91;view&#93;</strong></a></span><span class="view_on_open"><a href="#" onclick="return toggleContent(this)">&#91;hide&#93;</a>$1</span></span>');
		preg_match_all('/\[code\](.*?)\[\/code\]/si', $str, $code);
		foreach($code[1] as $stri)
			$str = str_replace('[', '&92;', $stri);
		$str = preg_replace($search, $replace, $str);
		return $str;
	}
?>

 

The replace works fine, but it shows the entity as opposed to it's special-character form.

 

So, this:

<?php
$str = formatText('[code][b]This[/b] is [i]italicized[/i] [u]text[/u][/ code]');
echo $str;
?>

 

Results in this:

&92;b]This&92;/b] is &92;i]italicized&92;/i] &92;u]text&92;/u]

 

Is there a way to actually get this to work? Or is there a completely different way to do this that will ensure its functionality?

 

A preemptive thanks for reading.

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.