0

I need to display a large amount of html as text(a table) which contains PHP variables. Now I would like to know if there is a command that echos the HTML as text, but keeps the PHP.

I know I can change the > with >. I'm just wondering if there is a better way to do this.

<table style="width:400px">
    <tr>
        <td><b style="font-size:27px;">$_POST['name']</b></td>  
        <td rowspan="2"> IMG</td>
    </tr>
    <tr>
        <td><b style="font-size:23px;font-weight:400">$_POST['function']</b></td>
    </tr>
</table>

This is only a part of it of course.

TL;DR: I want to echo the above text(as text, not code) but the variables from PHP need to execute as code.

4
  • A little (but accurate) example of the text you are talking about would help us give a useful answer. Commented Mar 9, 2015 at 15:29
  • What is the question and where is the PHP? Commented Mar 9, 2015 at 15:29
  • Updated question. Sorry, I need to work on asking better questions. Commented Mar 9, 2015 at 15:32
  • Please have a look at the Escaping from HTML chapter in the PHP manual. Commented Mar 9, 2015 at 15:41

2 Answers 2

3

Pushing it through the htmlentities function when you output the data should work. The function will convert whatever string you put in into one that looks exactly the same when rendered in HTML, which seems to be what you want.

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

Comments

1

Use htmlspecialchars and add curly braces around PHP variables:

$string = "<table style=\"width:400px\">
    <tr>
        <td><b style=\"font-size:27px;\">{$_POST['name']}</b></td>  
        <td rowspan=\"2\"> IMG</td>
    </tr>
    <tr>
        <td><b style=\"font-size:23px;font-weight:400\">{$_POST['function']}</b></td>
    </tr>
</table>";
echo htmlspecialchars($string);

1 Comment

Thanks for clarifying that I can also use curly braces instead of closing the string, variable and open string :), but since erik was first, I'll accept his answer

Your Answer

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