I've been working on a good replacement for the highlight_string() function; and here is what I've come up with so far:
<?
function get_sourcecode_string($str, $return = false, $counting = true, $first_line_num = '1', $font_color = '#666'){
$str = highlight_string($str, TRUE);
$replace = array(
'<font' => '<span',
'color="' => 'style="color: ',
'</font>' => '</span>',
'<code>' => '',
'</code>' => '',
'<span style="color: #FF8000">' =>
'<span style="color: '.$font_color.'">'
);
foreach ($replace as $html => $xhtml){
$str = str_replace($html, $xhtml, $str);
}
// delete the first <span style="color:#000000;"> and the corresponding </span>
$str = substr($str, 30, -9);
$arr_html = explode('<br />', $str);
$total_lines = count($arr_html);
$out = '';
$line_counter = 0;
$last_line_num = $first_line_num + $total_lines;
foreach ($arr_html as $line){
$line = str_replace(chr(13), '', $line);
$current_line = $first_line_num + $line_counter;
if ($counting){
$out .= '<span style="color:'.$font_color.'">'
. str_repeat(' ', strlen($last_line_num) - strlen($current_line))
. $current_line
. ': </span>';
}
$out .= $line
. '<br />'."\n";
$line_counter++;
}
$out = '<code>'."\n".$out.'</code>."\n"';
if ($return){return $out;}
else {echo $out;}
}
?>
This function outputs valid XHTML 1.1 code by replacing font tags with span tags. You can also specify whether you want it to return or echo, output a line-count, the color of the line-count, and the starting line-count number.
Usage:
<?
// $str = string with php
// $return = true (return) / false (echo)
// default of false
// $counting = true (count) / false (don't count)
// default of true
// $start = starting count number
// default of '1'
// $color = count color with preceding #
// defalut of '#666'
get_sourcecode_string($str, $return, $counting, $start, $color);
?>