• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • 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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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