php syntax highlight

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
alberto

php syntax highlight

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: php syntax highlight

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
alberto

ok now it's working

Post by alberto »

thank you, this is the best solution and works well.

:-)

bye
Post Reply