Forum Moderators: coopster

Message Too Old, No Replies

php bbcode problem

         

FiRe

11:25 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



I made this function:

function format($txt) {
$txt = preg_replace("/\[php\](.*?)\[\/php\]/is", "<div id=\"phpcode\">" . highlight_string("\\1", true) . "</div>", $txt);
return nl2br($txt);
}

This will output the code but it misses of the first "<?php" so it doesnt highlight the code! This is what the source looks like:

<div id="phpcode"><code><font color="#000000"><br />
<?php<br />
echo "test";<br />
?></font><br />
</code></div>

Any help?

eelixduppy

2:55 am on Jun 21, 2006 (gmt 0)



Are you sure you don't want this as your source code?:

<div id="phpcode"><code><font color="#000000">
[b][php][/b]
echo "test";
[b][/php][/b]</font>
</code></div>

This is what your regex seems to be searching for (I think--I'm still a beginner when it comes to Regular Expressions ;))

FiRe

8:43 am on Jun 21, 2006 (gmt 0)

10+ Year Member



[php]<?php
echo "test";
?>[/php]

Thats how it looks like and its missing out the first <?php and I dont know why :-S

proper_bo

9:21 am on Jun 21, 2006 (gmt 0)

10+ Year Member



I think you need to replace the < and > with &lt; and &gt; so the code doesn't get confused.

function format($txt) {
$txt = preg_replace("/<(.*?)>/is", "&lt;\\1&gt;", $txt);
$txt = preg_replace("/\[php\](.*?)\[\/php\]/is", "<div id=\"phpcode\">" . highlight_string("\\1", true) . "</div>", $txt);
return nl2br($txt);
}

try that. Might work.

FiRe

10:17 am on Jun 21, 2006 (gmt 0)

10+ Year Member



doesnt help, wont highlight if its already converted into entities...

proper_bo

2:42 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



you could change the highlight function to work with &lt; and &gt; as well as < and >

FiRe

3:04 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



no you cant its an inbuilt PHP function not custom made...

proper_bo

3:33 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



Oh yeah, forgot about it being a built in function.
I have read your code several times and I still can't find an error. Have you tried replacing the [php] with other letters like [abc] just to check it isn't that affecting the output?
Have you tried the match without using the highlight function to see what you are getting back in it's simplest form?

FiRe

6:59 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



changing it makes no difference and this will output the code:

$txt = preg_replace("/\[php\](.*?)\[\/php\]/is", "<div id=\"phpcode\">\\1</div>", $txt);