function bbcode_parse_code($stx, &$code)
{
switch (strtolower($stx)) {
case 'php':
$remove_tags = false;
$str_from = array('<', '>', '[', ']', '.', ':', ':');
$str_to = array('<', '>', '[', ']', '.', ':', ':');
$code = str_replace($str_from, $str_to, $code);
if (!preg_match('/\\<\\?.*?\\?\\>/is', $code)) {
$remove_tags = true;
$code = "<?php {$code} ?>";
}
$conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
foreach ($conf as $ini_var) {
@ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
}
// Because highlight_string is specialcharing the text (but we already did this before), we have to reverse this in order to get correct results
$code = htmlspecialchars_decode($code);
$code = highlight_string($code, true);
$str_from = array('<span style="color: ', '<font color="syntax', '</font>', '<code>', '</code>', '[', ']', '.', ':');
$str_to = array('<span class="', '<span class="syntax', '</span>', '', '', '[', ']', '.', ':');
if ($remove_tags) {
$str_from[] = '<span class="syntaxdefault"><?php </span>';
$str_to[] = '';
$str_from[] = '<span class="syntaxdefault"><?php ';
$str_to[] = '<span class="syntaxdefault">';
}
$code = str_replace($str_from, $str_to, $code);
$code = preg_replace('#^(<span class="[a-z_]+">)\\n?(.*?)\\n?(</span>)$#is', '$1$2$3', $code);
if ($remove_tags) {
$code = preg_replace('#(<span class="[a-z]+">)?\\?>(</span>)#', '$1 $2', $code);
}
$code = preg_replace('#^<span class="[a-z]+"><span class="([a-z]+)">(.*)</span></span>#s', '<span class="$1">$2</span>', $code);
$code = preg_replace('#(?:\\s++| )*+</span>$#u', '</span>', $code);
// remove newline at the end
if (!empty($code) && substr($code, -1) == "\n") {
$code = substr($code, 0, -1);
}
return "[code={$stx}:" . $this->bbcode_uid . ']' . $code . '[/code:' . $this->bbcode_uid . ']';
break;
default:
return '[code:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($code) . '[/code:' . $this->bbcode_uid . ']';
break;
}
}