Log in

View Full Version : Showing php in bbcode



insanemonkey
09-23-2007, 01:13 PM
Hi, I am trying to figure out how to show php in bbcode

on dd its

but I can figure out the bbcode for it, how to show it without it actin as a code.. can anyone help me please....



$message = str_replace("", "<code>", $message);
$message = str_replace("", "</code>", $message);


That is what i use for my bbcode but I dont know what to put into where the code tags are? please help me!

djr33
09-23-2007, 05:40 PM
http://www.dynamicdrive.com/forums/showthread.php?t=17549
And look at blm's post a bit down there, with a regular expressions version.

insanemonkey
09-23-2007, 06:09 PM
thats doesn't tell you how to show php in bbcode.. for example..


PHP code here <?php echo "what"; ?>

he doesnt have php tags in there, and i am trying to put php tags in my but no one knows how to do it wtf...?

djr33
09-23-2007, 06:30 PM
Oh, the highlighting?

http://www.php.net/manual/en/function.highlight-string.php

insanemonkey
09-23-2007, 09:54 PM
no not the highlighting...

the tags to show php!





but remove the lowercase p's..

I want to know what is the bbcode to show php in the forum.....

Twey
09-23-2007, 10:00 PM
What are you talking about? :confused:

In what forum? Show it how? You can show it by just typing <?php echo 'something like this, look, you can see this PHP code!'; ?> Is that what you want? Or do you not want to just show it? Do you want to do something else to it as well? Highlight it? Daniel showed you how to do that. If not highlighting, then what?

insanemonkey
09-23-2007, 10:05 PM
I have a forum on my site, ok...
I want users on my site be able to use tags on my site, but i can't find any code or php tutorial on it..

So users can post a php section.. Example below-

I want users to be able to do this, so they can put php code in here and show it on my forum!!!

I want users to be able to post php content.....
I want users to be able to post php content.....
I want users to be able to post php content.....

Twey
09-23-2007, 10:21 PM
You've been given all the code necessary to do this... just combine the BBCode code from post #2 with the highlighting from post #4.

insanemonkey
09-23-2007, 10:27 PM
yeh but post #2 does not have php tags in it.. omg how many times do i have to say that....

Twey
09-23-2007, 10:32 PM
You mean you want to use <?php ?> rather than ? I'm sure you can figure out how to replace one string with another...
omg how many times do i have to say that....That's a bit unfriendly, we're trying our best to work out what you're trying to say.

insanemonkey
09-23-2007, 10:35 PM
srry, but this driving me up the wall.

you know how you can use these tags

php] /php]

I want to be able to use them in my forum, but no one has an example or tutorial on how to show me...

i want to use php bbcode tags in my forum(I already have bbcode I just need a example)

uh sorry for being mean...

djr33
09-23-2007, 11:07 PM
Yeah... then use PHP in the code provided in the link in post 2. As tech_support posted, he added image tags himself. Just copy the format from the other ones.
That's it.

insanemonkey
09-24-2007, 04:24 AM
I have tried to make my own php tags but the php tags are different from the img tags, for example...

img tags look like this

", "<b>", $message);
$message = str_replace("[/b]", "</b>", $message);

$message = str_replace("", "<i>", $message);
$message = str_replace("", "</i>", $message);

$message = str_replace("
", "<div style=\"border: solid 1px orange; padding:20px; margin: 20px\"><code>", $message);
$message = str_replace("", "</code></div>", $message);

$message = str_replace("", "<u>", $message);
$message = str_replace("", "</u>", $message);

$message = str_replace("", "<s>", $message);
$message = str_replace("", "</s>", $message);

$message = str_replace("", "<span style=\"color: blue\">", $message);
$message = str_replace("", "</span>", $message);

$message = str_replace("", "<span style=\"color: red\">", $message);
$message = str_replace("", "</span>", $message);

$message = str_replace("", "<span style=\"color: green\">", $message);
$message = str_replace("", "</span>", $message);

$message = str_replace("", "<span style=\"color: yellow\">", $message);
$message = str_replace("", "</span>", $message);

$message = str_replace("", "<span style=\"color: gray\">", $message);
$message = str_replace("", "</span>", $message);

$message = str_replace("", "<span style=\"color: pink\">", $message);
$message = str_replace("", "</span>", $message);


/* here is where i get messed up and want the php tags */
$message = str_replace("[php}", "<php>", $message);
$message = str_replace("[/php}", "</php>", $message);
/* here is where i get messed up and want the php tags */


$message=eregi_replace("\\[url\\]www.([^\\[]*)\\[/url\\]","<a href=\"http://www.\\1\" target=_blank>\\1</a>",$message);
$message=eregi_replace("\\[url\\]([^\\[]*)\\[/url\\]","<a href=\"http://www.\\1\" target=_blank>\\1</a>",$message);
$message=eregi_replace("\\[url\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\" target=_blank>\\1</a>",$message);

$message=eregi_replace("\\[email\\]([^\\[]*)\\[/email\\]","<a href=\"mailto:\\1\">\\1</a>",$message);






$message = str_replace('[img]', '<img src="', $message);
$message = str_replace('', '" border=0>', $message);

$message = str_replace(":)", "<img src=\"http://www.xudas.com/forum/forum/images/smile2.gif\"><BR>", $message);

$message = str_replace(":(", "<img src=\"http://www.xudas.com/forum/forum/images/sad.gif\"><BR>", $message);

$message = str_replace(":\$", "<img src=\"http://www.xudas.com/forum/forum/images/confoosed.gif\"><BR>", $message);

?>


Oh and whereever you see this( "}" ) its supposed to be this ( "]"). code gets mess up..

djr33
09-24-2007, 04:48 AM
][/color]<? php; ?>[/php]
Becomes:
highlight_string('<? php; ?>');

ie,
$code
highlight_string($php);

And you get--
[php]<?php php; ?>

insanemonkey
09-24-2007, 12:52 PM
Finally!!!! THANKYOU SO MUCH!!!

I didn't know how or what to do...
I thanked you

insanemonkey
09-24-2007, 05:36 PM
Actually i can't get it to work, is there a possible way you could post it in this format..


$message = str_replace("[php}", "<?php php;{ ?>", $message);
$message = str_replace("[/php}", "<?php } ?>", $message);

Thats something I tried above and I have tried other solutions, is there a way you could show me a example using that above?

and its the same with other bbcode as in message is the text for the forums
like


$message=bbcode($message);


I hope you understand..

djr33
09-24-2007, 09:11 PM
It isn't just replacing the start/end with a tag. you need to run it through the function.

Basically--


while (strpos($c,'
')!==FALSE&&strpos($c,'',strpos($c,'
'))!==FALSE) {$c = substr($c,0,strpos($c,'[php]')).highlight_string(substr($c,strpos($c,strpos($c,'[php]'),strpos($c,'',strpos($c,'
'))))).substr($c,strpos($c,'',strpos($c,'[php]')));}

insanemonkey
09-24-2007, 09:13 PM
ok ty so much im gonna try this!!! TY

oh is
$c where $message is supposed to be?

djr33
09-24-2007, 09:42 PM
I just used $c for code since I didn't want to type a long variable name in that complex statement.


This is actually much harder than I thought it would be.

I've been testing it and the bugs are worked out, but there is a major problem.

Here's the code I have now:

while (strpos($c,'
')!==FALSE&&strpos($c,'',strpos($c,'
'))!==FALSE) { $c = substr($c,0,5+strpos($c,'[php]')).highlight_string(substr($c,strpos($c,5+strpos($c,'[php]'),6+strpos($c,'',strpos($c,'
')))),TRUE).substr($c,6+strpos($c,'',strpos($c,'[php]')));}


The problem, though, is the function highlight_string().

http://www.php.net/manual/en/function.highlight-string.php

Apparently it outputs rather than returning the highlighted code. A second parameter of TRUE is apparently supposed to make it return the code instead.
I set that... but... it doesn't seem to work.

Anyone used this before successfully?

Without the TRUE value, it outputs. With the TRUE value, I get a big memory allocation error--

Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 8855008 bytes) in /home/livechat/public_html/sys/test.php

Ideas?


Aside from that, though, the code works just fine.

thetestingsite
09-24-2007, 09:46 PM
I messed with it briefly before, but don't remember what I did exactly. I do remember; though, that I had to use the output buffer to get it to work right. That way everything parsed wrote to the ob, then I was able to output everything at the end of the script execution.

Hope this helps.

djr33
09-24-2007, 09:53 PM
Thanks.

Apparently the TRUE return value (second parameter) just enables a built in output buffer.

I added my own (and took off the return value), and I got an identical error message about memory allocation.

I'm just testing a single line of code with it.

I'm confused.

insanemonkey
09-24-2007, 09:53 PM
does it work without the highlight i couldn't get it workin to, If it works without the highlight then that would be awesome, I dont care about the highlighting...

Thankyou for helping me out!

djr33
09-24-2007, 10:12 PM
What would be the point of not highlighting it?

My code works fine. It simply doesn't apply the highlight_string function properly.

If you want to rewrite another function, go ahead and replace highlight_string() with it.

insanemonkey
09-24-2007, 10:32 PM
lol, I am giving up on this bbcode translation, I am going to wait until I know alot more about php, becuase this just confuses the heck out of me

djr33
09-25-2007, 06:19 AM
Here's a more understandable version of the code. If you want to play with it, go ahead. It works. I just don't know how to properly use the highlight function with an output buffer.


while (strpos($c,'
')!==FALSE&&strpos($c,'',strpos($c,'
'))!==FALSE) {
$d = substr($c,0,5+strpos($c,'[php]'));
ob_start();
highlight_string(substr($c,strpos($c,5+strpos($c,'[php]'),6+strpos($c,'',strpos($c,'
')))));
$d .= ob_get_clean();
$d .= substr($c,6+strpos($c,'',strpos($c,'[php]')));
$c = $d;
}


And here's that again, with detailed comments:

while (strpos($c,'
')!==FALSE&&strpos($c,'',strpos($c,'
'))!==FALSE) {
//while it is true that the PHP start tag exists in the string AND there is a PHP close tag [after that]:
$d = substr($c,0,5+strpos($c,'[php]'));
//set a new variable, $d to the first part of the string-- the part before the PHP open tag
ob_start();
//start the output buffer-- all output is stored into it and not sent to the browser
highlight_string(substr($c,strpos($c,5+strpos($c,'[php]'),6+strpos($c,'',strpos($c,'
')))));
//ok, this is the big confusing line-- i'll break it down a bit below...
//in short, get the part between the php tags and output that into the buffer as highlighted
$d .= ob_get_clean();
//add to the end of $d the contents of the output buffer and end the buffer
$d .= substr($c,6+strpos($c,'',strpos($c,'
')));
//add to the end of $d the last part of the string, starting at the first PHP close tag after the first PHP open tag
$c = $d;
//set $c to the new value, $d, for use in the next cycle of the loop or later
//$d was just temporary to get the parts out one at a time
}
//end loop

Ok, and here's a breakdown of that ugly line:
highlight_string(substr($c,strpos($c,5+strpos($c,'[php]'),6+strpos($c,'',strpos($c,'[php]')))));
>highlightthis([see below]) ...and output [part of the function automatically]
->get the part of the string $c that meets these conditions:
substr($c,5 after the position of the start tag, 6 after the position of the close tag after the start tag)
remember: substr(string,start at, length)

insanemonkey
09-25-2007, 04:48 PM
ty for this, i am gonna try it in a little bit...