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(
' '
'color="' => 'style="color: ',
'
' => '','' => '',
'' => '',
'' =>
''
);
foreach ($replace as $html => $xhtml){
$str = str_replace($html, $xhtml, $str);
}
// delete the first and the corresponding
$str = substr($str, 30, -9);
$arr_html = explode('
', $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 .= ''
. str_repeat(' ', strlen($last_line_num) - strlen($current_line))
. $current_line
. ': ';
}
$out .= $line
. '
'."\n";
$line_counter++;
}
$out = ''."\n".$out.'
."\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);
?>