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?
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);
Use PHP to set the content type:
header("Content-Type: text/plain");
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:
<body>
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.
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>