Page 1 of 1

Display PHP Code BBCode

Posted: Tue Mar 14, 2006 8:26 am
by blacksnday
I am working on making a BBCode type function
to display php code and am stuck so far.

What I got is:

Code: Select all

function phpTags($myphp)
{
    $myphp = preg_replace("#\[php\](.*?)\[/php\]#si", "\\1", $myphp);
    $showmyphp = "<div style='border: solid 1px'>". highlight_string($myphp) ."</div>";
    
     return($showmyphp);
}
which does correctly display the php code within the tags,
however it also highlights anything else within the total results
and does not limit to just within the tags.
and the <div> </div> shows at very bottom with the
1 being only thing inside it.

If I take away the highlight_string():
then it displays the Tagged Results within the <div> perfectly
however..(of course) any actual php code with the results isnt well-formed.

How to correct this?

Posted: Tue Mar 14, 2006 8:59 am
by blacksnday
Ok, i changed it to

Code: Select all

function phpTags($myphp)
{
    $myphp = preg_replace("#\[php\](.*?)\[/php\]#si", "\\1", $myphp);
    $showmyphp = "<div style='border: solid 1px'>". highlight_string('<?php '.$myphp.' ?>', true) ."</div>";
    
     return($showmyphp);
}
Which now shows within the <div> correctly
but still is highlighting everything from db results.

I cant seem to get it to only highlight between the

Code: Select all

[php] [/php]
tags

Posted: Tue Mar 14, 2006 9:31 am
by blacksnday
damn... so close!

now with

Code: Select all

function phpTags($myphp) 
{ 
    $myphp = preg_replace("#\[php\](.*?)\[/php\]#si", highlight_string("<?php \\1 ?>", true), $myphp); 

     return($myphp); 
}
it correctly recognizes the php tags but cant figure out how to correctly
use the needed \\1 as of now it throws errors

Posted: Tue Mar 14, 2006 10:27 am
by feyd
highlight_string() will be called before preg_replace will. Use preg_replace_callback().

Posted: Wed Mar 15, 2006 7:46 am
by blacksnday
I figured I would just use something that was posted
at php.net.

which brings me to a new question.
When returning highlight_string();
from a DB Query using nl2br();
The highlighted output also shows the Breaks.

I tried doing something like:

Code: Select all

$myphp = str_replace('<br />', '', $myphp);
however the breaks are still there.

Would it be wise to just not use nl2br();
and manually add it? Or can someone help me with
how to get them to not appear when highlighting php :)

p.s.
Current code I chose to use:

Code: Select all

function phpTags($myphp)
{
   $myphp = str_replace("]\n", "]", $myphp);
   $match = array('#\[php\](.*?)\[\/php\]#se');
   $replace = array("highlight_string( stripslashes('$1'), true  )");
   return preg_replace($match, $replace, $myphp);
}

Posted: Wed Mar 15, 2006 8:23 am
by feyd
don't nl2br() the code prior to highlighting.

it should be noted that using the "e" modifier with user input is a security hole.

Posted: Wed Mar 15, 2006 8:36 am
by blacksnday
feyd wrote:don't nl2br() the code prior to highlighting.

it should be noted that using the "e" modifier with user input is a security hole.
Thanks.

If I eventually allow users to submit code, I am warned :)