This is the same function I use to syntax highlight all of my code at this site. There's a lot of syntax highlighters out there, but I settled on the google-code-prettify because it is lightweight, customizable, simple, and developed and used by Google themselves. As always, Internet Explorer did their best to screw up the code, and this function addresses the fix by prefixing every newline with ' ' (an XML friendly ' ') for you.
<?php /* * author: Kyle Gadd * documentation: http://www.php-ease.com/functions/prettify.html * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ function prettify ($code, $lang='', $linenum=false) { global $page; $page->link(array('prettify.js', 'prettify.css')); $page->jquery('prettyPrint();'); $html = '<pre class="prettyprint'; if (!empty($lang) && in_array($lang, array('bsh', 'c', 'cc', 'cpp', 'cs', 'csh', 'cyc', 'cv', 'htm', 'html', 'java', 'js', 'm', 'mxml', 'perl', 'pl', 'pm', 'py', 'rb', 'sh', 'xhtml', 'xml', 'xsl'))) $html .= ' lang-' . $lang; if ($linenum !== false) { $html .= ' linenums'; if (is_numeric($linenum)) $html .= ':' . $linenum; $html .= '" style="padding-left:0px">'; } else { $html .= '">'; } $html .= str_replace(array('&', '<', '>', "\r\n", "\n"), array('&', '<', '>', "\n", " \n"), trim($code)); $html .= '</pre>'; return $html; } ?>