01-08-2008, 11:37 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
I wrote this code a while ago which you might find useful:
PHP Code:
<?php
function bbcode_phphighlight($info)
{
if(is_array($info) && !empty($info[1]))
{
if(!@ini_get('safe_mode'))
{
$info[1] = @highlight_string($info[1], true);
}
else
{
$temp = @tempnam(@ini_get('session.save_path'), 'phphighlightcache');
$fp = @fopen($temp, 'w');
if(!$fp)
{
return($info[1]);
}
fwrite($fp, $info[1]);
fclose($fp);
$info[1] = show_source($temp, true);
@unlink($temp);
}
/**
* For PHP4 to make XHTML support
*/
$info[1] = str_replace(Array(
'<font color="',
'">',
'</font>'
),
Array(
'<span style="color: ',
';">',
'</span>'
),
$info[1]
);
/**
* Style the output
*/
return(str_replace('<code>', '<code style="font: 11px Monaco, Courier, Monospace;">', $info[1]));
}
return($info[2]);
}
$string = "[php]<?php echo('Hello World'); ?>
";
$string = preg_replace_callback('#\[phpcode\](.*?)\[/phpcode\]#i', 'bbcode_phphighlight', $string);
echo($string);
?>[/php]
This should work under whatever options your php setup should have (running in safe mode or not). It will also convert old sloppy PHP4 highlights to nicely XHTML valid tags.
If you're looking into making your own syntax highlighting then I would really recommed you using the tokenizer extension avaible from:
PHP: Tokenizer - Manual
Kalle
Last edited by Kalle : 01-09-2008 at 01:58 PM.
|
|
|