Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Show PHP as text in a forum

    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 link="#"] Button Text [/button]

    Can anyone help me please?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,354
    Thanks
    61
    Thanked 529 Times in 516 Posts
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    $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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •