Jump to content

Archived

This topic is now archived and is closed to further replies.

scheols

PHP Syntax Highlighter

Recommended Posts

can someone show me the php highlihting so you can highlight php scripts

preg_replace() with highlight_string()

Share this post


Link to post
Share on other sites
If you want to highlight the php scripts, just just do this
for example:
[code]<?php
highlight_string('<?php
echo "Hello world!";
?>');
?>[/code]

Share this post


Link to post
Share on other sites
man i dont feel like doing that im trying 2 do BB codes

[code][code=php:0][/code][/code]

so yeah im trying to add it so users can do this.

Share this post


Link to post
Share on other sites
I find it easier to use a function called [url=http://uk.php.net/manual/en/function.preg-replace-callback.php]preg_replace_callback[/url] when doing PHP BBCode parsers. Heres a quick demo:
[code]<?php

function bbcode($txt)
{
    // bbcodes
    $bbcodes = array( "|\[b\](.+)\[/b\]|is",
                      "|\[u\](.+)\[/u\]|is",
                      "|\[i\](.+)\[/i\]|is"
                    );

    // html
    $replace = array( "<strong>$1</strong>",
                      "<u>$1</u>",
                      "<em>$1</em>"
                    );

    $text = preg_replace($bbcodes, $replace, $txt);

    // call a dedicated function to parse our PHP BBCodes:
    $text = preg_replace_callback("#\[php\](.*?)\[\/php\]#is", 'doPHP', $text);

    return nl2br($text);

}

// our dedicated PHP BBCode function
function doPHP($matches)
{
    #echo '<pre>' . print_r($matches, true) . '</pre>';

    //highlight PHP Code
    $php = highlight_string($matches[0], true);

    // remove the mataches
    unset($mataches);

    // remove the php BBCodes
    $php = preg_replace("#(\[php\]|\[/php\])#i", "", $php);

    //clean up spaces and extra line breaks
    $php = str_replace('&nbsp;&nbsp;', '&nbsp; ', $php);
    $php = str_replace('<br />', '', $php);

    return $php;
}

$str = "[code=php:0]<?php
echo 'hello world';

if(\$var == 'hello world')
{
    echo 'true';
}
?>[/code]";

// call the bbcode parser function.
$str = bbcode($str);

echo $str;

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.