Frage

I have this html in a string:

$html = '<obj><p>Figure 1. different (<italic>p</italic>< 0.05).</p></obj>';

Then, I am loading this in a domDocument:

$doc  = new DOMDocument("1.0","UTF-8");
@$doc->loadHTML($html);

Then, when I am dumping the content of the domDocument:

var_dump($doc->saveHTML());

I am getting:

<html><body><obj><p>Figure 1. different (<italic>p</italic></p></obj></body></html>

So th sign < and the rest disappeared.

Any idea why?

Thank you.

War es hilfreich?

Lösung

This will print as xml

header("Content-type: text/xml; charset=utf-8");
$html = '<obj><p>Figure 1. different (<italic>p</italic>'.    htmlspecialchars('< 0.05).') .'</p></obj>';

// Or else if you need this, then uncomment below line

//$html = htmlspecialchars('<obj><p>Figure 1. different (<italic>p</italic>< 0.05).</p></obj>');
$doc  = new DOMDocument("1.0","UTF-8");
@$doc->loadHTML($html);
echo ($doc->saveHTML());

Andere Tipps

The parser thinks you are opening a new HTML tag. Try using &lt; instead.

$html = '<obj><p>Figure 1. different (<italic>p</italic>&lt; 0.05).</p></obj>';

Well, that < is used by the html markup, thus the html string you post is interpreted by browsers as html.

If you want to show the literal html markup, you will have to escape it or mark it as preformatted text in an explicit manner: :

echo "<pre>\n";
var_dump($doc->saveHTML());
echo </pre\n";

If you want the html markup to be interpreted, but just have single characters escaped, you have to do that in an explicit manner, so that the browser can tell the difference:

$html = '<obj><p>Figure 1. different (<italic>p</italic>&lt; 0.05).</p></obj>';
var_dump($html);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top