How to display PHP code in textarea?

Discussion in 'PHP' started by lokielookies, Aug 5, 2008.

  1. #1
    In a text area, I want to show the PHP code for a script, but I only see the HTML output, instead of the plain source code.

    How can I get this plain PHP code to display?
     
    lokielookies, Aug 5, 2008 IP
  2. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    For this purpose, I use a small Javascript component, that not only is very usefull, it also embeded the textarea into a nice IDE design with print and copy paste functionnalities.

    You can see a sample and download it at http://code.google.com/p/syntaxhighlighter/
     
    webrickco, Aug 5, 2008 IP
  3. somanweb

    somanweb Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use html special characters in the text in the content.

    for eg.

    <form action="test.php" method="post">
    <textarea rows="5" cols="25" >
    &lt;?php

    echo "Welcome to the world";

    ?&gt;
    </textarea>
    </form>

    Try this one
     
    somanweb, Aug 5, 2008 IP
  4. lokielookies

    lokielookies Well-Known Member

    Messages:
    1,246
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    150
    #4
    I have been struggling with this for such a long time... if only I had known it was this easy :rolleyes:

    Thanks for the help!

    ;)
     
    lokielookies, Aug 5, 2008 IP
  5. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <textarea rows="x" cols="y" >
    <?php
    highlight_file('/path/to/your_source_file.ext');
    ?>
    </textarea>
    
    Code (PHP):
    OR
    
    <textarea rows="x" cols="y" >
    <?php
    $var = "Your source PHP here. Attention to \" splash or others like \$ , etc.";
    highlight_string($var);
    ?>
    </textarea>
    
    Code (PHP):
     
    lovelycesar, Aug 5, 2008 IP
  6. Le4rner

    Le4rner Active Member

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #6
    
    <textarea>
    <?php
    echo '<php 
    //This is the PHP code I want to output
    echo "All this code will appear in plain text because it is in single quotes";
    ';
    </textarea>
    
    HTML:
    It is easy as you can see. You should also search for phtml files and this php functions to output a files source in syntax highlighted format.

    http://us.php.net/manual/en/function.highlight-file.php
     
    Le4rner, Aug 5, 2008 IP
  7. spyka

    spyka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    My website has a code snippets section and I use the following code to output the php codes in a textarea:

    
    <textarea>
    <?php
    echo(htmlspecialchars('<?php echo 'foo'; ?>'));
    ?>
    </textarea>
    
    PHP:
     
    spyka, Aug 5, 2008 IP
  8. Le4rner

    Le4rner Active Member

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #8
     
    Le4rner, Aug 5, 2008 IP