Link to home
Start Free TrialLog in
Avatar of bahadirkocaoglu
bahadirkocaogluFlag for Türkiye

asked on

New BB Tag: [php] .. [/php]

Hi,

I need a custom bb tag.

Like:
[php]
<?php
echo 'test';
?>
[/php]

How can I do this?

Thanks.

I have currenty this code:
<?php
function parsemymessage($string) {
 
	// destroy slashes
	$string = stripslashes($string);
 
	// destroy html tags
	$string = strip_tags($string);
 
	// create smileys
	$string = str_replace(":)", "<img src=\"images/smileys/smiley.gif\" alt=\":)\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":D", "<img src=\"images/smileys/cheesy.gif\" alt=\":D\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(";)", "<img src=\"images/smileys/wink.gif\" alt=\";)\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(";D", "<img src=\"images/smileys/grin.gif\" alt=\";D\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace("???", "<img src=\"images/smileys/huh.gif\" alt=\"???\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":(", "<img src=\"images/smileys/sad.gif\" alt=\":(\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":o", "<img src=\"images/smileys/shocked.gif\" alt=\":o\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":`(", "<img src=\"images/smileys/cry.gif\" alt=\":`(\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":*", "<img src=\"images/smileys/kiss.gif\" alt=\":*\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":X", "<img src=\"images/smileys/lipsrsealed.gif\" alt=\":X\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
	$string = str_replace(":p", "<img src=\"images/smileys/tongue.gif\" alt=\":p\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
 
	// bb codes
	$search = array(
			'/\[b\](.*?)\[\/b\]/is',
			'/\[i\](.*?)\[\/i\]/is',
			'/\[u\](.*?)\[\/u\]/is',
			'/\[s\](.*?)\[\/s\]/is',
			'/\[code\](.*?)\[\/code\]/is',
			'/\[quote\](.*?)\[\/quote\]/is',
			'/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
			'/\[sub\](.*?)\[\/sub\]/is',
			'/\[sup\](.*?)\[\/sup\]/is',
			'/\[img\](.*?)\[\/img\]/is',
			'/\[url\](.*?)\[\/url\]/is',
			'/\[url\=(.*?)\](.*?)\[\/url\]/is'
	);
	$replace = array(
			'<strong>$1</strong>',
			'<em>$1</em>',
			'<u>$1</u>',
			'<s>$1</s>',
			'<div><b>Code:</b></div><div id="code">$1</div>',
			'<div><b>Quote:</b></div><div id="quote">$1</div>',
			'<div><b>Quote from: $1</b></div><div id="quote">$2</div>',
			'<sub>$1</sub>',
			'<sup>$1</sup>',
			'<img src="$1" />',
			'<a href="$1">$1</a>',
			'<a href="$1">$2</a>'
	);
	$string = preg_replace($search, $replace, $string);
 
	$search2 = array('(\[php\](.*?)\[/php\])se');
	$replace2 = array("''.highlight_string('$1', true).''");
	$string = preg_replace($search2, $replace2, $string);
 
	// \n or \r to <br>
	$string = nl2br($string);
 
	// destory spaces
	$string = trim($string);
 
	// return string
	return $string;
}
?>

Open in new window

Avatar of mr_egyptian
mr_egyptian
Flag of United States of America image

Add the following lines to $search and $replace.  I just used pre tags in the replace, but you can format it however you'd like.

        // bb codes
        $search = array(
                        '/\[b\](.*?)\[\/b\]/is',
                        '/\[i\](.*?)\[\/i\]/is',
                        '/\[u\](.*?)\[\/u\]/is',
                        '/\[s\](.*?)\[\/s\]/is',
                        '/\[code\](.*?)\[\/code\]/is',
                        '/\[quote\](.*?)\[\/quote\]/is',
                        '/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
                        '/\[sub\](.*?)\[\/sub\]/is',
                        '/\[sup\](.*?)\[\/sup\]/is',
                        '/\[img\](.*?)\[\/img\]/is',
                        '/\[url\](.*?)\[\/url\]/is',
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',
                        '/\[php\](.*?)\[\/php\]/is'
        );
        $replace = array(
                        '<strong>$1</strong>',
                        '<em>$1</em>',
                        '<u>$1</u>',
                        '<s>$1</s>',
                        '<div><b>Code:</b></div><div id="code">$1</div>',
                        '<div><b>Quote:</b></div><div id="quote">$1</div>',
                        '<div><b>Quote from: $1</b></div><div id="quote">$2</div>',
                        '<sub>$1</sub>',
                        '<sup>$1</sup>',
                        '<img src="$1" />',
                        '<a href="$1">$1</a>',
                        '<a href="$1">$2</a>',
                        '<pre>$1</pre>'
        );

Open in new window

Avatar of Roonaan
Adding such tags is a HUGE security risk. Are you sure you need it? If it's for admin's only you could possibly better use something like [php]somesnippetid[/php] and then use a filesystem or database snippet library with tested and secured code snippets.
Avatar of bahadirkocaoglu

ASKER

I want this:

[php]
<?php
echo 'test';
?>
[/php]

So:
[php] ... [/php] is a string, and I want print this:

highlight_string($myString, true);
For this to work you need to look into an alternative approach. the strip_tags is stripping out your php blocks. Therefor you would to have to pre-parse the php pieces.

Try this:
class php_highlight {
	private static $blocks = array();
	private static $counter = 0;
	static function register_block($block) {
		$i = self::$counter++;
		self::$blocks[$i] = $block;
		return '[php]#block'.($i).'[/php]';
	}
	static function retrieve_block($id) {
		ob_start();
		echo '<blockquote><fieldset><legend>php code:</legend>';
		highlight_string(self::$blocks[$id]);
		echo '</fieldset></blockqoute>';
		return ob_get_clean();;
	}
}
 
function parsemymessage($string) {
 
        // destroy slashes
        $string = stripslashes($string);
 
        // pre-remove php blocks:
        $string = preg_replace('/\[php](.*?)\[\/php]/ise', 'php_highlight::register_block("$1")', $string);
        
        // destroy html tags
        $string = strip_tags($string, '<?php');
 
        // create smileys
        $string = str_replace(":)", "<img src=\"images/smileys/smiley.gif\" alt=\":)\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":D", "<img src=\"images/smileys/cheesy.gif\" alt=\":D\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(";)", "<img src=\"images/smileys/wink.gif\" alt=\";)\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(";D", "<img src=\"images/smileys/grin.gif\" alt=\";D\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace("???", "<img src=\"images/smileys/huh.gif\" alt=\"???\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":(", "<img src=\"images/smileys/sad.gif\" alt=\":(\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":o", "<img src=\"images/smileys/shocked.gif\" alt=\":o\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":`(", "<img src=\"images/smileys/cry.gif\" alt=\":`(\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":*", "<img src=\"images/smileys/kiss.gif\" alt=\":*\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":X", "<img src=\"images/smileys/lipsrsealed.gif\" alt=\":X\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
        $string = str_replace(":p", "<img src=\"images/smileys/tongue.gif\" alt=\":p\" width=\"18\" height=\"18\" border=\"0\" style=\"vertical-align: middle;\" />", $string);
 
        // bb codes
        $search = array(
                        '/\[b\](.*?)\[\/b\]/is',
                        '/\[i\](.*?)\[\/i\]/is',
                        '/\[u\](.*?)\[\/u\]/is',
                        '/\[s\](.*?)\[\/s\]/is',
                        '/\[code\](.*?)\[\/code\]/is',
                        '/\[quote\](.*?)\[\/quote\]/is',
                        '/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
                        '/\[sub\](.*?)\[\/sub\]/is',
                        '/\[sup\](.*?)\[\/sup\]/is',
                        '/\[img\](.*?)\[\/img\]/is',
                        '/\[url\](.*?)\[\/url\]/is',
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',
                        '/\[php\]#block(\d+)\[\/php\]/ise'
        );
        $replace = array(
                        '<strong>$1</strong>',
                        '<em>$1</em>',
                        '<u>$1</u>',
                        '<s>$1</s>',
                        '<div><b>Code:</b></div><div id="code">$1</div>',
                        '<div><b>Quote:</b></div><div id="quote">$1</div>',
                        '<div><b>Quote from: $1</b></div><div id="quote">$2</div>',
                        '<sub>$1</sub>',
                        '<sup>$1</sup>',
                        '<img src="$1" />',
                        '<a href="$1">$1</a>',
                        '<a href="$1">$2</a>',
                        'php_highlight::retrieve_block("$1")'
        );
        $string = preg_replace($search, $replace, $string);
 
        //$search2 = array('(\[php\](.*?)\[/php\])se');
        //$replace2 = array("''.highlight_string('$1', true).''");
        //$string = preg_replace($search2, $replace2, $string);
 
        // \n or \r to <br>
        $string = nl2br($string);
 
        // destory spaces
        $string = trim($string);
 
        // return string
        return $string;
}
 
 
$message = 'test [b]bold[/b][php]<'.'?php echo time();'.'?'.'>[/php]';
 
echo htmlspecialchars($message).'<hr />';

Open in new window

Doesn't work ...
Are you on php4 or php5?
PHP 5
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial