<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fishbite</title>
    <description>The latest articles on DEV Community by Fishbite (@fishbite).</description>
    <link>https://dev.to/fishbite</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2756547%2Fb046748f-d956-48b7-ae42-e11583f2f9e0.png</url>
      <title>DEV Community: Fishbite</title>
      <link>https://dev.to/fishbite</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fishbite"/>
    <language>en</language>
    <item>
      <title>HTML Parsing with PHP 8.4's New DOM\HTMLDocument Class</title>
      <dc:creator>Fishbite</dc:creator>
      <pubDate>Tue, 22 Jul 2025 14:37:05 +0000</pubDate>
      <link>https://dev.to/fishbite/html-parsing-with-php-84s-new-domhtmldocument-class-3005</link>
      <guid>https://dev.to/fishbite/html-parsing-with-php-84s-new-domhtmldocument-class-3005</guid>
      <description>&lt;h2&gt;
  
  
  Intro: PHP 8.4's New &lt;code&gt;DOM\HTMLDocument&lt;/code&gt; Class
&lt;/h2&gt;

&lt;p&gt;PHP 8.4 quietly introduced a powerful new class: &lt;a href="https://www.php.net/manual/en/class.dom-htmldocument.php" rel="noopener noreferrer"&gt;&lt;code&gt;DOM\HTMLDocument&lt;/code&gt;&lt;/a&gt;. Unlike the traditional &lt;code&gt;DOMDocument&lt;/code&gt;, this class is tailor-made for parsing &lt;strong&gt;HTML5&lt;/strong&gt;, not XML, and brings a more forgiving and intuitive DOM interface to modern PHP.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This requires &lt;strong&gt;PHP 8.4+&lt;/strong&gt; and the &lt;code&gt;dom&lt;/code&gt; extension to be enabled.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What's the Difference?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;DOM\HTMLDocument&lt;/code&gt; is more HTML5-native than the classic &lt;code&gt;DOMDocument&lt;/code&gt;, which is XML-centric and strict about malformed markup. If you’ve ever fought with &lt;code&gt;DOMDocument&lt;/code&gt; silently failing or auto-correcting tags, this new API is for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sample Project on GitHub
&lt;/h2&gt;

&lt;p&gt;I created a GitHub repo showing how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load and parse HTML5 files&lt;/li&gt;
&lt;li&gt;Access elements like &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Extract &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tags&lt;/li&gt;
&lt;li&gt;Cleanly render DOM elements as HTML&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/Fishbite/php-dom-htmldocument-examples" rel="noopener noreferrer"&gt;github.com/Fishbite/php-dom-htmldocument-examples&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Example: Extracting the First Element in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;DOM\HTMLDocument&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="k"&gt;__DIR__&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$dom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HTMLDocument&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;createFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'./html/sample.html'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$dom&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'head'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$head&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;childNodes&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$child&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$child&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;DOM\Element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"First tag in &amp;lt;head&amp;gt;: &amp;lt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$child&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tagName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$dom&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;saveHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What's Included in the Repo?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;extract_head.php: Show the first child of &lt;/li&gt;
&lt;li&gt;extract_title.php: Grab the  text&lt;/li&gt;
&lt;li&gt;list_meta.php: List all  tags&lt;/li&gt;
&lt;li&gt;sample.html: Minimal HTML5 doc for testing&lt;/li&gt;
&lt;li&gt;composer.json: Basic autoloading config&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can run each file individually from the command line or route them through a browser if preferred.&lt;/p&gt;




&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;There’s very little official documentation or community content around DOM\HTMLDocument, so this is a good time to explore it together. Feedback, suggestions, or PRs are more than welcome!&lt;/p&gt;

&lt;p&gt;Let’s help get the word out and make working with HTML in PHP a whole lot nicer.&lt;/p&gt;

</description>
      <category>php</category>
      <category>domdocument</category>
      <category>domhtmldocument</category>
      <category>parsinghtml</category>
    </item>
  </channel>
</rss>
