PHP DomDocument behaving differently in CLI and Web Browser
I 'm using the following code:
$doc = new DOMDocument();
$doc->loadHTML("<i><p><strong>From: fsong | #001</strong><br/>I hate you DomDocument :(.</p></i><br/>you'd be surprised<br/>");
echo $doc->saveHTML();
Running it in the CLI gets me
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<i><p><strong>From: fsong | #001</strong><br>I hate you DomDocument :(.</p></i><br>you'd be surprised<br>
</body></html>
while running it through the web browser returns:
Warning: DOMDocument::loadHTML(): Unexpected end tag : i in Entity, line: 1 in /home/xx/www/test/开发者_Go百科topic_archiver_test.php on line 50
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<i></i><p><strong>From: fsong | #001</strong><br>I hate you DomDocument :(.</p>
<br>you'd be surprised<br>
</body></html>
Now I realize nesting a (p) tag inside an (i) tag breaks HTML rules, but I'm not the one responsible for the HTML. For some reason the CLI mode returns the document like the original while the web page version closes the (i) tag early to keep the HTML well-formed.
Is there something in my php.ini configurations that is causing the difference in behaviours? I checked the official docs (http://www.php.net/manual/en/dom.configuration.php) and there doesn't seem to be any configuration files or settings for DomDocument.
Looks like for some reason it is trying to validate the HTML. I'm not getting any errors running php 5.3.5 and libxml 2.7.6.
One way to ignore the warning is prepending a '@' character, of course this is kind of a hack.
@$dom->saveHTML();
You could try turning off strictErrorChecking from DOMDocument
精彩评论