update page now

Voting

: one plus three?
(Example: nine)

The Note You're Voting On

zero
20 years ago
In some cases, I found that it's useful to have highlight_string format <code>...</code> inline as part of a paragraph, and other times, as a block for demonstrating multiple lines of code. I made this function to help out.

<?php
    function highlight_code($code, $inline=false, $return=false) // Pre php 4 support for capturing highlight
    {
        (string) $highlight = "";
        if ( version_compare(phpversion(), "4.2.0", "<") === 1 )
        {
            ob_start(); // start output buffering to capture contents of highlight
            highlight_string($code);
            $highlight = ob_get_contents(); // capture output
            ob_end_clean(); // clear buffer cleanly
        }
        else
        {
            $highlight=highlight_string($data, true);
        }
        
        ## The classes below need to correspond to a stylesheet!
        if ( $inline === true )
          $highlight=str_ireplace("<code>","<code class=\"inline\">",$highlight);
        else
          $highlight=str_ireplace("<code>","<code class=\"block\">",$highlight);
            
        
        if ( $return === true )
        {
            return $highlight;
        }
        else
        {
            echo $highlight;
        }
    }
?>

<< Back to user notes page

To Top