How can the combination of preg_replace() and GeSHi be optimized to effectively highlight and display code snippets within forum posts?

To optimize the combination of preg_replace() and GeSHi for highlighting code snippets within forum posts, you can use preg_replace_callback() instead of preg_replace() to apply GeSHi highlighting to code blocks. This allows for more flexibility and control over the highlighting process.

// Function to apply GeSHi highlighting to code blocks
function highlightCode($matches) {
    $geshi = new GeSHi(trim($matches[2]), $matches[1]);
    return $geshi->parse_code();
}

// Regular expression to match code blocks
$pattern = '/\[code=(\w+)\](.*?)\[\/code\]/s';

// Sample forum post content with code snippets
$postContent = "Here is a code snippet: [code=php]echo 'Hello, World!';[/code]";

// Apply GeSHi highlighting to code blocks in the forum post content
$highlightedContent = preg_replace_callback($pattern, 'highlightCode', $postContent);

// Output the highlighted forum post content
echo $highlightedContent;