note 50069 added to function.highlight-string

From: Date: Thu, 17 Feb 2005 07:15:16 +0000
Subject: note 50069 added to function.highlight-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-85148@lists.php.net to get a copy of this message
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('&nbsp;', 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); ?> ---- Manual Page -- http://www.php.net/manual/en/function.highlight-string.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50069 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50069&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50069&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50069&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50069&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50069&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50069&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85148) next »