Jump to content


Photo

PHP Syntax Highlighter


  • Please log in to reply
3 replies to this topic

#1 scheols

scheols
  • Members
  • PipPipPip
  • Advanced Member
  • 113 posts

Posted 01 August 2006 - 04:53 AM

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

preg_replace() with highlight_string()
Dont Piss me Off today

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 06:18 AM

If you want to highlight the php scripts, just just do this
for example:
<?php
highlight_string('<?php
echo "Hello world!";
?>');
?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 scheols

scheols
  • Members
  • PipPipPip
  • Advanced Member
  • 113 posts

Posted 01 August 2006 - 06:37 AM

man i dont feel like doing that im trying 2 do BB codes

[code=php:0][/code]

so yeah im trying to add it so users can do this.
Dont Piss me Off today

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 August 2006 - 10:12 AM

I find it easier to use a function called preg_replace_callback when doing PHP BBCode parsers. Heres a quick demo:
<?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;

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users