Req #47875 [Opn->Ver]: No option to set HTML input encoding

From: Date: Fri, 23 Oct 2020 15:22:50 +0000
Subject: Req #47875 [Opn->Ver]: No option to set HTML input encoding
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229885@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=47875&edit=1

 ID:                 47875
 Updated by:         cmb@php.net
 Reported by:        thomas dot koch at ymc dot ch
 Summary:            No option to set HTML input encoding
-Status:             Open
+Status:             Verified
 Type:               Feature/Change Request
 Package:            DOM XML related
 Operating System:   Debian Lenny
 PHP Version:        5.2.9
 Block user comment: N
 Private report:     N

 New Comment:

Not a solution, but likely a viable workaround would be prepending
the HTML string with a BOM ("\xef\xbb\xbf" for UTF-8), see
<https://3v4l.org/ArhNb>.


Previous Comments:
------------------------------------------------------------------------
[2018-07-22 18:36:11] anrdaemon at freemail dot ru

Prefixing does not work.

The default input encoding of DOMDocument is IS-8859-1 contrary to the documentation that says the
input should be UTF-8 encoded.

If you prefix your document with "<?xml …", it will change mode to UTF-8
regardless of encoding specified in the XML declaration, and mangle the declaration itself.

https://3v4l.org/HL5It

In short, DOMDocument is largely unusable for HTML, only well-formed XML with explicit declaration
gives you a small hope of success.

------------------------------------------------------------------------
[2015-12-22 21:19:12] nathan dot renniewaldock at gmail dot com

This really does need to be supported. Though libxml2 is partly to blame for ignoring <meta
charset="utf-8">

For now, workaround is to prefix the HTML with either
<?xml version="1.0" encoding="UTF-8"?>
or
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

------------------------------------------------------------------------
[2015-07-10 15:55:59] cmb@php.net

Related To: Bug #49705

------------------------------------------------------------------------
[2013-01-07 17:34:42] crmalibu at gmail dot com

I also stumbled upon libxml2's htmlSetMetaEncoding() here:

http://www.xmlsoft.org/encoding.html#implemente
and
http://www.xmlsoft.org/html/libxml-HTMLtree.html


This would be a very welcome feature addition. Currently, hacky php code like this festers in the
wild due to the lack of being able to specify the encoding:

$encodingHint = '<meta http-equiv="Content-Type" content="text/html;
charset=utf-8">';
$dom->loadHTML($encodingHint . $html); // lol make it utf8

or maybe some str_replace() or use of html tidy if the developer was feeling robust that day... 

This really sucks, because to me it looks like the functionality is totally there in libxml2.

------------------------------------------------------------------------
[2012-07-04 08:02:05] julien at go-on-web dot com

I have another test case for you, using HTML5 :


<?php


// ----- 
// FAIL CASE

$html = <<<HTML
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <p id="accent">Test case with simple accent (&eacute;) : é</p>
  </body>
</html>
HTML;
		
$doc = new DomDocument( 1.0, 'UTF-8' );
$doc->loadHTML( $html );

var_dump( $doc->getElementById('accent')->textContent );

//=> string(40) "Test case with simple accent (é) : é" 
// ----



// -----
// SUCCESS CASE (but invalid html5)

$html = <<<HTML
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  </head>
  <body>
    <p id="accent">Test case with simple accent (&eacute;) : é</p>
  </body>
</html>
HTML;

$doc = new DomDocument( 1.0, 'UTF-8' );
$doc->loadHTML( $html );

var_dump( $doc->getElementById('accent')->textContent );

//=> string(38) "Test case with simple accent (é) : é"
// -----

?>


Regards, 
Julien

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=47875


--
Edit this bug report at https://bugs.php.net/bug.php?id=47875&edit=1


Thread (9 messages)

« previous php.bugs (#229885) next »