View Full Version : Show PHP as text in a forum

01-06-2012, 03:34 PM
I would like to show PHP and HTML code in a forum. Therefore I must display the code as plain text. But everytime the website executes the PHP code and won't display it.

I have seen PHP code boxes around on websites, these can also work for me.

For example, i want to show the following code as plain tekst:
Button Text

Can anyone help me please?


01-06-2012, 03:47 PM
If the PHP is executing, you haven't specified that it is a string. Whether it is a string or comes from a database, the only way to execute it beyond the scope of the immediate script is with an eval. So the first step will be making it into an actual string.
To display the string, it is the same as displaying HTML. Use htmlspecialchars() in order to convert the tags so that it can be displayed without a parse.

01-06-2012, 04:37 PM
Thanks for the reply, I do not understand it completely.

Is there no code like <textbox> PHP Code </textbox> which I can add while composing the new thread so that the code is displayed?

01-06-2012, 04:39 PM
You could do that yes but that won't stop php from running the php code - that will just determine how it looks on the clients browser.

Put the php code through htmlspecialchars() first as Fou has suggested. If the php is being executed on the server before printing it to the browser then you need to stop that from happening.

01-06-2012, 04:41 PM
Textarea is a html property. Your job is to make sure that anything between the textarea tags is readable as plain text. If you use something like HTML or PHP within the textarea, you have to replace the characters appropriately so that it can be displayed as text and not HTML.

$string = '<?php echo "hello world";?>'; // <> indicates HTML tags if left in this format

printf('<textarea>%s</textarea>', htmlspecialchars($string));

The textarea should now render the PHP code as text and not an element.