We help IT Professionals succeed at work.

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

bahadirkocaoglu
on
557 Views
Last Modified: 2008-11-22
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

Comment
Watch Question

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

CERTIFIED EXPERT
Top Expert 2006

Commented:
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.

Author

Commented:
I want this:

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

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

highlight_string($myString, true);
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
Doesn't work ...
CERTIFIED EXPERT
Top Expert 2006

Commented:
Are you on php4 or php5?

Author

Commented:
PHP 5
CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.