1

I am trying to create a preview HTML division wherein I face a challenge. When I try to get code from the textarea and print it in the HTML, I could not see the PHP code. Here's are my codes, HTML Where the code from textarea will be printed

<pre id='pques'></pre> 

jQuery code that will take value from textarea and put in the above HTML pretty print area:

jQuery(document).ready(function($) {
    setInterval(function(){
        $("#pques").html($("#eques").val());
    } 
});

Value inside the textarea with id=eques

What will be the output of the following php code? <div class="code"><?php $num = 1; $num1 = 2; print $num . "+". $num1 ; ?> </div>

Someone kindly help me to achieve this. I want somewhat similar functionality to the stack overflow question preview stuff.

Thank you.

2
  • Have a look at this answer. You can use .text() instead of .html() Commented Mar 21, 2016 at 14:11
  • empiric, This looks way better. Thank you. But I want only the PHP codes to be escaped. The <div class="code"></div> must be printed as HTML and <?php $num... ?> must be printed as PHP plain text. Might be a work with RegExp. Help me. Commented Mar 21, 2016 at 14:21

2 Answers 2

1

The simplest way to do this is to escape the angle brackets in the PHP open and closing tags as HTML entities:

<?php .. ?> becomes &lt;?php ... ?&gt; - PHP will not treat this as server code and will ignore it, but the browser will display &lt; as < and &gt; as >. This way you don't need JavaScript to do it, you can print it directly to the page. I would advise always HTML encoding any PHP code that is input on your site, and storing it with entities rather than executable code.

Sign up to request clarification or add additional context in comments.

Comments

0

As you use jquery, you can achieve this as described here

This will escape the brackets as @kallum-tanton already pointed out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.