RE: Presenting code snipetts in standard PHP colours

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PHP's website (http://www.php.net) has a great solution to displaying source
code.  If you view the source code on their site you can see how they do it.
It's just a basic function to loop through the text and place in code to
highlight in various places.  In fact here is that portion of code:

function highlight_php($code, $return = FALSE)
{
   // Using OB, as highlight_string() only supports
   // returning the result from 4.2.0
   ob_start();
   highlight_string($code);
   $highlighted = ob_get_contents();
   ob_end_clean();

   // This should eventually be a php_syntax_check() call when we move to
PHP5
   // But use this ugly hack for now to avoid code snippets with bad syntax
screwing up the highlighter
   if(strstr($highlighted,"include/layout.inc</b>")) $highlighted =
"<pre>".htmlentities($code)."</pre>";

   // Fix output to use CSS classes and wrap well
   $highlighted = '<div class="phpcode">' . str_replace(
       array(
           '&nbsp;',
           '<br />',
           '<font color="',        // for PHP 4
           '<span style="color: ', // from PHP 5.0.0RC1
           '</font>',
           "\n ",
           '  '
       ),
       array(
           ' ',
           "<br />\n",
           '<span class="',
           '<span class="',
           '</span>',
           "\n&nbsp;",
           '&nbsp; '
       ),
       $highlighted
   ) . '</div>';

   if ($return) { return $highlighted; }
   else { echo $highlighted; }
}

This is how they called that function:
highlight_php(join("", file($page_name)));


Original Message-----
From: Ross Honniball [mailto:ross@xxxxxxxxxxxxxxxxxxx]
Sent: Thursday, December 09, 2004 6:42 AM
To: php windows
Subject:  Presenting code snipetts in standard PHP colours


Hi all,

What is the easiest way to get a web page to list source code with standard
colour high-lighting (as used in my php_manual_el.CHM file and as you see
on various php web pages about the place).

Is there a class or a function or something that anyone knows of?

Thanks ... Ross
.
. Ross Honniball. JCU Bookshop Cairns, Qld, Australia.
.

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux