简体   繁体   中英

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/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();

您可以尝试从DOMDocument关闭strictErrorChecking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM