• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

<xmp> equivalent

<pre> will not display the HTML code - how can i display HTML code without using the <xmp> tag or a textarea?
0
benwiggy
Asked:
benwiggy
  • 3
  • 2
1 Solution
 
BatalfCommented:
I think you have to use the special characters &lt;

example:

<pre>
&lt;h1>This is a heading&lt;/h1>
</pre>

or change the content of a pre tag dynamically.

example:


<pre id="myID">
<p>THis is a simple test</p>
<h2>A heading</h2>
</pre>
<script type="text/javascript">
var obj = document.getElementById('myID');
obj.innerHTML = obj.innerHTML.replace(/</g,'&lt;');
</script>
0
 
Joakim_Commented:
Do you mean that this:

<pre><b>Bold text</b></pre>

...Returns bold text, and you want it to return literal "<>"s? If so, use this:

<pre>&lt;b&gt;Bold text&lt;/b&gt;</pre>
0
 
benwiggyAuthor Commented:
Interesting solution Batalf, but I think I will use the less than greater than signs I ended up using anyway.

But the Javascript one is interesting...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BatalfCommented:
Glad I could help!

The javascript solution is especially useful if you have alot of text and don't have access to use server side scripts for the conversion(< to &lt; ).

Batalf

0
 
benwiggyAuthor Commented:
If you notice this, an example of a PHP server side solution?
0
 
BatalfCommented:
There are several PHP solutions.

If the content is in a string you could just use str_replace() on it

example:

<?
$string = "<h1>This is a heading</h1><p>This is a paragraph</p>";
$string = str_replace("<","&lt;",$string);
echo $string);
?>

You can also use the buffer functions. This means that you collect the buffer and don't output anything to the screen. Then you collect the content of the buffer and save it as a variable.

Example:

<?php
ob_start(); // Start output buffering
?>
<h1>This is a heading</h1>
<p>THis is a paragraph</p>
<p>This is also a paragraph</p>
<?
$data = ob_get_contents(); // Put the content of the buffer into the variable $data
ob_end_clean(); // Clean the buffer without outputting anything

$data = str_replace("<","&lt;",$data); // replace < with &lt;

echo $data;

?>
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now