Display PHP Code BBCode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Display PHP Code BBCode

Post 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?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

highlight_string() will be called before preg_replace will. Use preg_replace_callback().
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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 :)
Post Reply