2

How can stop HTML code from rendering and display it as standard text, so my users can simply copy and paste it for their own usage?

4 Answers 4

9

You could use the function htmlentities(). Have a look at the manual.

Please note that by default the data will be returned using the ISO-8859-1 character set. You might want to change the third parameter to UTF-8.

For example:

$html = "<div>Some text</div>";

echo htmlentities($html);
Sign up to request clarification or add additional context in comments.

Comments

4

Use PHP to set the content type:

header("Content-Type: text/plain");

1 Comment

Yes listen to Michiel Pater, my answer is not what you are looking for (I don't think so at least). This would serve the entire page as plain text.
1

You parse it into markup which uses HTML entities, for example instead of using:

<body>

you would instead use entity codes to escape the markup characters, like this:

&lt;body&gt;

There are plenty of references to be found which list entity codes, and it's pretty easy to parse and escape HTML in most programming languages.

Comments

0

HTML code can be displayed using php script by using functions htmlentities() and htmlspecialchars(). For understanding this concept consider the following example:

<?php
$a="<h1>heading tag: used for heading tags</h1>";
echo htmlentities($a)."<br/>";
echo htmlspecialchars($a);
?>

Output will be:

<h1>heading tag: used for heading tags</h1>
<h1>heading tag: used for heading tags</h1>

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.