phptextblogsgeshi

Detect code for GeSHi


I'm making a blog from scratch in PHP + MySQL, where I will sometimes post lines of PHP codes for tutorials and so on... So, I installed the GeSHi class in my project. But since I will write my blogs/articles in normal text format, I will write up some tutorials like:

Here is some PHP code:
[code=php]
<?php
echo 'Hello World!';
?>
[/code]

And this will be stored on my MySQL DB in TEXT format, so how can I set PHP to recognize code's and use the GeSHi class to parse it?


Solution

  • Yo can do that by simple regular replace:

    function code($match) {
        $geshi = new GeSHi($match[2], $match[1]);
        return $geshi->parse_code();
    }
    $html = preg_replace_callback('~\[code=(.+?)\](.+?)\[\/code\]~is', 'code', $text);