33

I have a string with XML:

$string = 
"
<shoes>
    <shoe>
       <shouename>Shoue</shouename>
    </shoe>
</shoes>
";

And would like display it on my website like this:

This is XML string content:
<shoes>
    <shoe>
       <shouename>Shoue</shouename>
    </shoe>
</shoes>

So I would like to do it:

  • on site, not in textbox
  • without external libraries, frameworks etc.
  • formatted with proper new lines
  • formatted with tabs
  • without colors etc., only text

So how to do it in plain and simple way?

5 Answers 5

67

If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:

<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

@anto.nishanth firstly echo is a language construct so it don't need to be represented as function. Then here Chris use multiple parameter construct and you are talking about concatenation in to a single parameter. Please read documentation for further details php.net/echo.
6

you can use htmlentities(), htmlspecialchars() or some similar function.

Comments

6

It should work like that:

echo '<p>This is XML string content:</p>'
echo '<pre>';
echo htmlspecialchars($string);
echo '</pre>';

Comments

1

If it is a SimpleXMLobject

<pre>
<?php
echo htmlspecialchars(print_r($obj,true));
?>
</pre>

Comments

1

I searched for a solution to have slightly coloured output:

$escaped = htmlentities($content);
$formatted = str_replace('&lt;', '<span style="color:blue">&lt;', $escaped);
$formatted = str_replace('&gt;', '&gt;</span>', $formatted);
echo "<pre>$formatted</pre>\n";

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.