search for in the  
<highlight_fileignore_user_abort>
Last updated: Thu, 19 May 2005

highlight_string

(PHP 4, PHP 5)

highlight_string -- Syntax highlighting of a string

Description

mixed highlight_string ( string str [, bool return] )

The highlight_string() function outputs a syntax highlighted version of str using the colors defined in the built-in syntax highlighter for PHP.

If the second parameter return is set to TRUE then highlight_string() will return the highlighted code as a string instead of printing it out. If the second parameter is not set to TRUE then highlight_string() will return TRUE on success, FALSE on failure.

Example 1. highlight_string() example

<?php
highlight_string
('<?php phpinfo(); ?>');
?>

The above example will output (in PHP 4):

<code><font color="#000000">
<font color="#0000BB">&lt;?php phpinfo</font><font color="#007700">(); </font><font color="#0000BB">?&gt;</font>
</font>
</code>

The above example will output (in PHP 5):

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php phpinfo</span><span style="color: #007700">(); </span><span style="color: #0000BB">?&gt;</span>
</span>
</code>

Note: The return parameter became available in PHP 4.2.0. Before this time it behaved like the default, which is FALSE

See also highlight_file().



User Contributed Notes
highlight_string
support at superhp dot de
10-Apr-2005 07:58
With this function you can highlight php code with line numbers:

<?php
function highlight_php($string)
{
 
$Line = explode("\n",$string);

  for(
$i=1;$i<=count($Line);$i++)
  {
  
$line .= "&nbsp;".$i."&nbsp;<br>";
  }
  
 
ob_start();
 
highlight_string($string);
 
$Code=ob_get_contents();
 
ob_end_clean();
 
 
$header='<table border="0" cellpadding="0" cellspacing="0" width="95%" style="border-style: solid; border-width:1px; border-color: white black black white">
   <tr>
     <td width="100%" colspan="2"  style="border-style: solid; border-width:1px; border-color: white; background-color: #99ccff; font-family:Arial; color:white; font-weight:bold;">Php-Code:</td>
   </tr>
   <tr>
     <td width="3%" valign="top" style="background-color: #99ccff; border-style: solid; border-width:1px; border-color: white;"><code>'
.$line.'</code></td>
     <td width="97%" valign="top" style="background-color: white;"><div style="white-space: nowrap; overflow: auto;"><code>'
;

 
$footer=$Code.'</div></code></td>
   </tr>
  </table>'
;

  return
$header.$footer;
}
?>
admin [at] develogix [dot] com
17-Feb-2005 02:15
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);
?>
gaggge at gmail dot com
30-Jan-2005 11:26
This is a little function for highlighting bbcode-stylish PHP code from a mysql database.
(Like this: [php]<?php echo "test"; ?>[/php])

<?php
function bbcode($s)
{
  
$s = str_replace("]\n", "]", $s);
  
$match = array('#\[php\](.*?)\[\/php\]#se');
  
$replace = array("'<div>'.highlight_string(stripslashes('$1'), true).'</div>'");
   return
preg_replace($match, $replace, $s);
}
?>
admin at bwongar dot com
05-Jan-2005 06:11
I didn't get the expected results from the other XHTML_highlight function, so I developed my own and it is much more efficient. The older one uses a preg_replace to replace the contents of the tag to within a span tag. The only preg_replace in my function pulls the color attribute, and puts it within a str_replace'd span tag.

<?php
function xhtml_highlight($str) {
  
$str = highlight_string($str, true);
  
$str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
   return
preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
}

?>
manithu at fahr-zur-hoelle dot org
06-Nov-2004 02:10
This function will return highlighted, xhtml 1.1 valid code (replaces <font> with <span> elements and color with style attributes):

<?php

function xhtml_highlight($str) {
  
$str = highlight_string($str, true);
  
//replace <code><font color=""></font></code>
  
$str = preg_replace('#<font color="([^\']*)">([^\']*)</font>#', '<span style="color: \\1">\\2</span>', $str);
  
//replace other <font> elements
  
return preg_replace('#<font color="([^\']*)">([^\']*)</font>#U', '<span style="color: \\1">\\2</span>', $str);
}

?>
mastah_br at yahoo dot com
11-Oct-2004 11:31
If you're looking for a more generic syntax highlighter, then you should see GeSHi Project, at http://qbnz.com/highlighter/
aidan at php dot net
26-Sep-2004 11:29
To add line numbers to source code, with optional function linking, use the below function:

http://aidan.dotgeek.org/lib/?file=function.highlight_file_linenum.php

A much more thorough and smarter, though slower version is here:

http://aidan.dotgeek.org/lib/?file=PHP_Highlight.php

<highlight_fileignore_user_abort>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT