Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-07-2006, 08:46 AM   PM User | #1
Curtis D
New Coder

 
Join Date: Jun 2006
Location: USA
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Curtis D is an unknown quantity at this point
PHP 4 and 5 compatible source highlighter

I wrote this function when I was writing my PHP quiz.

mixed highlight_code( string $code, bool $class, bool $return );

More verbose highlight_string. This will either echo or return the code (returns by default). The $class argument will allow you to insert a CSS class in the highlighted PHP's <code> tags. This is useful if you want to highlight code in both a block element and inline elements.

PHP Code:
<?php
    
function highlight_code($code$class=false$return=true)
    {
        if ( empty(
$code) ) return false;

        
$highlight '';
        if ( 
version_compare(PHP_VERSION'4.2.0''<') )
        {
            
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($codetrue);
        }
        
        if ( 
$class )
            
$highlight=preg_replace('/<code>/i',"<code class=\"${class}\">"$highlight);

        if ( 
$return )
            return 
$highlight;
        else
            echo 
$highlight;
    }
?>
Created by Curtis Dyer
__________________
"Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
--Jamie Zawinski
Curtis D is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:09 PM.


Advertisement
Log in to turn off these ads.