-5

Is there a way to output PHP code as plain text? I have PHP code stored in my mysqli database, I want to fetch the PHP code and display it in a div. I don 't want the PHP code to be executed. Or is there a way to use file_put_contents() to fill a file with the PHP code? Again; I don 't want the PHP code to be executed. I either want the PHP code to be displayed as plain text or to be put in a file as plain text.

For example I have this code stored in my database:

<DOCTYPE>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
  <?php
    $array = array('test1', 'test2');

    foreach($array as $val)
      print($val);

    print('works!');
  ?>
  </body>
</html>

But when I print this code the entire PHP code won't be visible.

Or is there a way to put that PHP code from my database into a file using file_put_contents

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Henk de B
  • 55
  • 1
  • 8
  • Have you tried something so far? Maybe the search? http://stackoverflow.com/questions/4842575/how-do-i-display-php-code-in-html – t.h3ads Jul 27 '15 at 12:53
  • you look for http://php.net/manual/en/function.highlight-file.php ... maybe your server is configured that if you rename showme.php to showme.php**s** it will automatically show the source – donald123 Jul 27 '15 at 12:54

2 Answers2

2

If the code is stored in the database, all you have to do is echo it in the div. Run your query, get the string, echo. It won't be executed. You will probably need to run it through htmlentities to get things like <, & and quotes to show up on the page correctly.

Jessica
  • 7,075
  • 28
  • 39
-1

There are 2 built-in functions of PHP that might be of interest in this respect. They are highlight_string() and highlight_file()

highlight_file()

Prints out or returns a syntax highlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP.

http://php.net/manual/en/function.highlight-file.php

highlight_string ()

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.

http://php.net/manual/en/function.highlight-string.php

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Since the OP specifically said he was storing the PHP code in a database, and not in a file, this doesn't seem relevant. It is a very nice function for code highlighting, but that's not what he asked about. This should be a comment. – Jessica Jul 27 '15 at 13:34
  • The OP has it stored in a db and wishes to display in a div - hence mentioning the two functions - with highlight_string() being of particular use in this instance I would have thought as the code is not to be executed, only displayed!? – Professor Abronsius Jul 27 '15 at 13:37
  • I don't want any fancy higlighting, just plain text. – Henk de B Jul 27 '15 at 14:11