// Format a block of code into a readable usable format.
function mod_format_php_code($data)
{
global $context;
// Convert all line endings to html returns.
$data = preg_replace('~[\r]?\n~', "
", $data);
// This part was borrowed from SMF. This is basically the code that handles formating the code bbc.
$php_parts = preg_split('~(<\?php|\?>)~', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
{
// Do PHP code coloring?
if ($php_parts[$php_i] != '<?php')
continue;
$php_string = '';
while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != '?>')
{
$php_string .= $php_parts[$php_i];
$php_parts[$php_i++] = '';
}
$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
}
// Fix the PHP code stuff...
$data = str_replace("<pre style=\"display: inline;\">\t", "\t", implode('', $php_parts));
// Older browsers are annoying, aren\'t they?
if ($context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t", $data);
elseif (!$context['browser']['is_gecko'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
else if (!$context['browser']['is_firefox'])
$data = str_replace("\t", " ", $data);
// Firefox doesn't understand line breaks in <code> tags very well.
if ($context['browser']['is_firefox'])
return '<pre language="php" class="php_code">' . $data . '';
return '<code>' . $data . '</code>';
}
// Format code properly for the code box.
function mod_format_php_code($data)
{
global $context;
// Convert all line endings to html returns.
// !!! Expect for opera which has a fit about it.
if (!$context['browser']['is_opera'])
$data = preg_replace('~[\r]?\n~', "
", $data);
// Older browsers are annoying, aren\'t they?
if ($context['browser']['is_ie4'] || $context['browser']['is_ie5'] || $context['browser']['is_ie5.5'])
$data = str_replace("\t", "<pre style=\"display: inline;\">\t", $data);
elseif (!$context['browser']['is_gecko'])
$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
else if (!$context['browser']['is_firefox'])
$data = str_replace("\t", " ", $data);
// Firefox doesn't understand line breaks in <code> tags very well.
elseif ($context['browser']['is_firefox'])
return '<pre language="php" class="php_code">' . $data . '';
return '<code>' . $data . '</code>';
}