Page 1 of 1

php syntax highlight

Posted: Wed Mar 09, 2005 1:49 pm
by alberto
does anybody know about how to highlight php code in s9y?

I mean I would like to post something like:
<?php

// comment

$foo = 'bar';

?>
also I would php code to be highlighted by highlight_string() php function.

I have a way to do that, but it's not clear to me how to bundle it within s9y source and if it has some limitation because of something I'm not aware of.

here is the idea. when printing an entry you should do:
$entry = preg_replace_callback('/<\?php(.*)\?>/s', 'entry_highlight_code', $entry);
also should be set a function like:
function entry_highlight_code($matches) {
// <pre> tags is preferred to <code> and there are too many <p> tags,
// but there's no way to override highlight_string() behaviour,
// so I used the following workaround
return sprintf("<pre>%s</pre>", preg_replace('/<br \/>|<p>|<\/p>|<code>|<\/code>/', '',
highlight_string($matches[0], true)));
}
is there any phpBB tag that could be used as well?

bye, alberto.

Re: php syntax highlight

Posted: Wed Mar 09, 2005 5:04 pm
by garvinhicking
You can use the [php] bbcode tag:

Code: Select all

// comment
$foo = 'bar';
?>
Make sure that the nl2br plugin is listed AFTER the BBCode plugin, or you will get strange <br /> inside your code.

Regards,
Garvin

ok now it's working

Posted: Thu Mar 10, 2005 10:57 am
by alberto
thank you, this is the best solution and works well.

:-)

bye