Skip to content

libxml update on Linux systems changes behavior of DOMDocument->loadHTML #24349

Closed as duplicate

Description

@DanielRuf

Description

The following code:

<?php
	public function clean($html)
	{
...
                $doc = new \DOMDocument();
		$doc->loadHTML(
			'<?xml encoding="utf-8" ?><body>'. $valid .'</body>',
			LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
		);
		
		$this->cleanNode($doc->getElementsByTagName('body')->item(0));

		$doc->formatOutput = true;
		$filtered = $doc->saveHTML();
		
		$clean = str_replace(array('<?xml encoding="utf-8" ?><body>', '</body>'), '', $filtered);
...
}		

Resulted in this output:

<!--?xml encoding="utf-8" ?--><body>
 <span>test

But I expected this output instead:

<span>test

It seems it now handles HTML code and does not need this XML trick anymore (because libxml now seems to convert <?xml to <!--?xml .
So what is the right solution to have the same result for PHP 8.1 - PHP 8.5?

PHP Version

PHP 8.5.9-dev (cli) (built: Jun 21 2026 23:58:16) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.5.9-dev, Copyright (c) Zend Technologies
    with Xdebug v3.5.0, Copyright (c) 2002-2025, by Derick Rethans
    with Zend OPcache v8.5.9-dev, Copyright (c), by Zend Technologie

Operating System

Ubuntu 26.04

Activity

  1. ndossche commented on Oct 15, 2026

    @ndossche
    Member

    This is effectively a duplicate of #24300, please see the discussion there (also for broader context).

  2. DanielRuf commented on Oct 15, 2026

    @DanielRuf
    ContributorAuthor

    Perfect, thank you very much for pointing me to the existing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions