Bug #70450 [Opn->Wfx]: DOM::loadHTML() --> ::saveXML() entity encodes CR

From: Date: Fri, 12 Mar 2021 18:51:58 +0000
Subject: Bug #70450 [Opn->Wfx]: DOM::loadHTML() --> ::saveXML() entity encodes CR
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232680@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70450&edit=1 ID: 70450 Updated by: cmb@php.net Reported by: flavio dot cambraia at yahoo dot com dot br Summary: DOM::loadHTML() --> ::saveXML() entity encodes CR -Status: Open +Status: Wont fix Type: Bug Package: DOM XML related Operating System: Windows PHP Version: 5.5.29 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: The culprit is reading HTML and writing as XML. libxml2 escapes CR when writing XML because normally CRLF is folded to LF when reading XML (required for conforming parsers). As such, this behavior is not wrong per se. I consider the presented use-case as quite uncommon, and I don't think this should be worked around in PHP, especially since you can work around this by not having CRLF as line endings, or if you have no control over the documents, still can fold the CRLF manually (e.g. by using str_replace()). Previous Comments: ------------------------------------------------------------------------ [2015-09-07 14:24:58] cmb@php.net This has nothing to do with XPath. Rewriting the code to use "classic" DOM methods yields the same result. ------------------------------------------------------------------------ [2015-09-07 14:00:02] flavio dot cambraia at yahoo dot com dot br Description: ------------ The output shows an entity &#13; for every line break in $contents var. I am using PHP 5.5.8 build date Jan 8 2014 15:26:26 Test script: --------------- <?php $contents = '<!DOCTYPE html> <html lang="pt"><head><meta charset="utf-8"></head> <body> <div class="div_entry"> <div class="div_imagem"> <a href="test.html"> <img src="test.png" alt="" />Link to imagem</a> </div> <div class="div_product">This is a title for a product</div> <div class="div_price">R$50,00</div> </div>'; $doc = new DOMDocument(); $doc->loadHTML('<?xml encoding="UTF-8">' . $contents); $xpath = new DOMXpath($doc); $xquery = '//div[@class="div_entry"]'; $articles = $xpath->query($xquery); $registros = array(); foreach ($articles as $i => $article) { $registros[] = $article->ownerDocument->saveXML($article); } // end foreach echo "<pre>"; print_r($registros); echo "</pre>"; ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70450&edit=1

« previous php.bugs (#232680) next »