syntax highlighting for PHP code

dharmil

New Member
Messages
1,656
Reaction score
0
Points
0
i need help creating a bbcode that will higlight php and html code
so it colored unlike regular CODE tag
like
[php ] code [/ php]
and it will look like this:
PHP:
<?php
echo "Hello World!";
?>
and some for html
[html ] code [/ html]
HTML:
<input type="text" name="from" size="24" />
and it has to work with this:
PHP:
 function bbcode($message){ 
     $search = array('#\[b\](.*?)\[/b\]#s',
                     '#\[img\](.*?)\[/img\]#s',
                     '#\[i\](.*?)\[/i\]#s',
                     '#\[u\](.*?)\[/u\]#s',
                     '#\[s\](.*?)\[/s\]#s',
                     '#\[q\](.*?)\[/q\]#s',
                     '#\[c\](.*?)\[/c\]#s',
                     '#\[url=(.*?)\](.*?)\[/url\]#s',
                     '#\[email=(.*?)\](.*?)\[/email\]#s',
                     '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})\](.*?)\[/color\]#s',
                     '#\[---\]#s',
                     '#\[h\](.*?)\[/h\]#',
                     '#\[size=(.*?)\](.*?)\[/size\]#si',
                     '#\[font=(.*?)\](.*?)\[/font\]#',
                     '#\[align=(.*?)\](.*?)\[/align\]#',
                     '#\[style=(.*?)\](.*?)\[/style\]#',
                     '#\[code\](.+?)\[/code\]#s',
                     '#\[br\]#s' );
    $replace = array('<strong>$1</strong>',
                     '<a href="$1"><img src="http://dharmil.info/thumb.php?img=$1" border="0" /></a><br /><a href="$1">Click to Enlarge (+)</a><br />',
                     '<em>$1</em>',
                     '<ins>$1</ins>',
                     '<del>$1</del>',
                     '<q>$1</q>',
                     '<code>$1</code>',
                     '<a href="$1">$2</a>',
                     '<a href="mailto:$1">$2</a>',
                     '<span style="color: $1">$2</span>',
                     '<hr />',
                     '<span style="background-color: #FFFF00; color: #000000">$1</span>',
                     '<font size=\'$1\'>$2</font>',
                     '<span style="font-family: $1">$2</span>',
                     '<div align="$1">$2</div>',
                     '<span style="$1">$2</span>',
                     '<div class="code-top"><strong>Code:</strong></div><div class="code"><div class="codeinside">$1</div></div>',
                     '<br />' );
     $bbcodeize = ''.preg_replace($search, $replace, $message).'';
    return $bbcodeize;
}
 
Last edited:

The_Magistrate

New Member
Messages
1,118
Reaction score
0
Points
0
I think you're looking for highlight_string()[url]. You can use it to highlight a string of PHP in an echo. You would need something like this:

PHP:
function bbcode($message){ 
     $search = array('#\[b\](.*?)\[/b\]#s',
                     '#\[img\](.*?)\[/img\]#s',
                     '#\[i\](.*?)\[/i\]#s',
                     '#\[u\](.*?)\[/u\]#s',
                     '#\[s\](.*?)\[/s\]#s',
                     '#\[q\](.*?)\[/q\]#s',
                     '#\[c\](.*?)\[/c\]#s',
                     '#\[url=(.*?)\](.*?)\[/url\]#s',
                     '#\[email=(.*?)\](.*?)\[/email\]#s',
                     '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})\](.*?)\[/color\]#s',
                     '#\[---\]#s',
                     '#\[h\](.*?)\[/h\]#',
                     '#\[size=(.*?)\](.*?)\[/size\]#si',
                     '#\[font=(.*?)\](.*?)\[/font\]#',
                     '#\[align=(.*?)\](.*?)\[/align\]#',
                     '#\[style=(.*?)\](.*?)\[/style\]#',
                     '#\[code\](.+?)\[/code\]#s',
                     '#\[br\]#s',
                     '#\[php\](.+?)\[/php\]#s' );
    $replace = array('<strong>$1</strong>',
                     '<a href="$1"><img src="http://dharmil.info/thumb.php?img=$1" border="0" /></a><br /><a href="$1">Click to Enlarge (+)</a><br />',
                     '<em>$1</em>',
                     '<ins>$1</ins>',
                     '<del>$1</del>',
                     '<q>$1</q>',
                     '<code>$1</code>',
                     '<a href="$1">$2</a>',
                     '<a href="mailto:$1">$2</a>',
                     '<span style="color: $1">$2</span>',
                     '<hr />',
                     '<span style="background-color: #FFFF00; color: #000000">$1</span>',
                     '<font size=\'$1\'>$2</font>',
                     '<span style="font-family: $1">$2</span>',
                     '<div align="$1">$2</div>',
                     '<span style="$1">$2</span>',
                     '<div class="code-top"><strong>Code:</strong></div><div class="code"><div class="codeinside">$1</div></div>',
                     '<br />',
                     highlight_string($1) );
     $bbcodeize = ''.preg_replace($search, $replace, $message).'';
    return $bbcodeize;
}

You may have to tweak the above code, because I haven't tested it, but it should work.
 
Top