PDA

View Full Version : Creating - [php]code[/php]


Mhtml
04-25-2003, 08:54 PM
I'm having a load of trouble trying to get this script to work.

function phpcode($str)
{
$str = str_replace('\\"','"',$str);
$str = highlight_string($str,1);
return $str;
}
$message = preg_replace('/\[php\](.*?)\[\/php\]/ise', "phpcode('\\\\$1')", $message);


It is supposed to do exactly as the [ php ] tags in this forum do but it just doesn't seem to want to do it sometimes.

It doesn't seem to handle \ very well, because if you enter " in the message field of the form and submit it gets turned into \" and it comes up with an error saying unexpected character \" !! But what's odd is it is always the last quote, the first quotes get changed but the second qoute in the string doesn't it's left as \". So there is code there to turn \" into ' " ' but it just doesn't seem to work more than 50% of the time.

chrisvmarle
04-25-2003, 09:37 PM
I got it working with this line:
$message = preg_replace("/\[php\](.*?)\[\/php\]/ise", "phpcode(\"$1\")", $message);

Mzzl, Chris
Hey! Where did my slashed go?
$message = preg_replace("/\[php\](.*?)\[\/php\]/ise", "phpcode(\"$1\")", $message);

Mhtml
04-25-2003, 09:49 PM
It works for strings like echo "test" but if I do echo 'test' it throws an error:

<?php "Dude"
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\phpdev\www\fmb\bbcode.php on line 17
n
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\phpdev\www\fmb\bbcode.php on line 17
n ?>

String: <?php "Dude"
"test"
?>

I whent to a newline in my textarea to see if it handled newlines and it didn't.

chrisvmarle
04-25-2003, 10:06 PM
Hm... I see ...

I've been playing with
stripslashes($str);

but I can't find a solution (yet), sorry

Mzzl, Chris

Mhtml
04-25-2003, 10:13 PM
Well I passed stripslashes over $message before I did the conversion and it worked for <?php echo 'test' ?> but <?php echo "test" ?> didn't work... :confused:

Phantom
04-25-2003, 10:56 PM
no semi-colons maybe?

Mhtml
04-25-2003, 10:58 PM
But don't you need the semi colon to denote the end of the line/statement? Or is there some big php secret I'm yet to learn?

Phantom
04-25-2003, 11:33 PM
In PHP, the semicolon denotes the end of a statement. If you echo something in PHP, it has to end with a semi-colon (as opposed to just doing <?=$var?>)

blah blah blah this is what I'm going to echo <? echo "test" ?>

wont' work

but <? echo "test"; ?> will

:-)

Mhtml
04-25-2003, 11:35 PM
That's what I thought . . .

Phantom
04-25-2003, 11:46 PM
Reading your post again, what I mean by "no semi-colons" was to point out that the code you had supplied didn't have any semicolons after your echo statements

Mhtml
04-25-2003, 11:58 PM
I fixed it!! :D W00T!!!
function phpcode($str)
{
$str = highlight_string(stripslashes($str),1);
return $str;
}

Added stripslashes() to the $str in the highlight_string() which seems to have calmed the beast.. . new lines work " work ' work.. perfect. :)

Thanks peoples for the help. :)