Slicing HTML based on delimiter : <p>I am converting Word docs on the fly to HTML and needing to parse said HTML based on a delimiter. For example:</p>

<pre><code>&lt;div id="div1"&gt;
    &lt;p&gt;
        &lt;font&gt;
            &lt;b&gt;[[delimiter]]Start of content section 1.&lt;/b&gt;
        &lt;/font&gt;
    &lt;/p&gt;
    &lt;p&gt;
        &lt;span&gt;More content in section 1&lt;/span&gt;
    &lt;/p&gt;
&lt;/div&gt;
&lt;div id="div2"&gt;
    &lt;p&gt;
        &lt;b&gt;
            &lt;font&gt;[[delimiter]]Start of section 2&lt;/font&gt;
        &lt;/b&gt;
    &lt;p&gt;
    &lt;span&gt;More content in section 2&lt;/span&gt;
    &lt;p&gt;&lt;font&gt;[[delimiter]]Start of section 3&lt;/font&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div id="div3"&gt;
    &lt;span&gt;&lt;font&gt;More content in section 3&lt;/font&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;!-- This continues on... --&gt;
</code></pre>

<p>Should be parsed as:</p>

<p>Section 1:</p>

<pre><code>&lt;div id="div1"&gt;
    &lt;p&gt;
        &lt;font&gt;
            &lt;b&gt;[[delimiter]]Start of content section 1.&lt;/b&gt;
        &lt;/font&gt;
    &lt;/p&gt;
    &lt;p&gt;
        &lt;span&gt;More content in section 1&lt;/span&gt;
    &lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>Section 2:</p>

<pre><code>&lt;div id="div2"&gt;
    &lt;p&gt;
        &lt;b&gt;
            &lt;font&gt;[[delimiter]]Start of section 2&lt;/font&gt;
        &lt;/b&gt;
    &lt;p&gt;
    &lt;span&gt;More content in section 2&lt;/span&gt;
    &lt;p&gt;&lt;/p&gt;
&lt;div&gt;
</code></pre>

<p>Section 3:</p>

<pre><code>&lt;div id="div2"&gt;
    &lt;p&gt;
        &lt;b&gt;

        &lt;/b&gt;
    &lt;p&gt;
    &lt;p&gt;&lt;font&gt;[[delimiter]]Start of section 3&lt;/font&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div id="div3"&gt;
    &lt;span&gt;&lt;font&gt;More content in section 3&lt;/font&gt;&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<ol>
<li><p>I can't simply "explode"/slice based on the delimiter, because that would break the HTML. Every bit of text content has many parent elements.</p></li>
<li><p>I have no control over the HTML structure and it sometimes changes based on the structure of the Word doc. An end user will import their Word doc to be parsed in the application, so the resulting HTML will not be altered before being parsed.</p></li>
<li><p>Often the content is at different depths in the HTML.</p></li>
<li><p>I cannot rely on element classes or IDs because they are not consistent from doc to doc. #div1, #div2, and #div3 are just for illustration in my example.</p></li>
<li><p>My goal is to parse out the content, so if there's empty elements left over that's OK, I can simply run over the markup again and remove empty tags (p, font, b, etc).</p></li>
</ol>

<p>My attempts:</p>

<p>I am using the PHP DOM extension to parse the HTML and loop through the nodes. But I cannot come up with a good algorithm to figure this out.</p>

<pre><code>$doc = new \DOMDocument();
$doc-&gt;loadHTML($html);
$body = $doc-&gt;getElementsByTagName('body')-&gt;item(0);

foreach ($body-&gt;childNodes as $child) {
    if ($child-&gt;hasChildNodes()) {
        // Do recursive call...
    } else {
        // Contains slide identifier?
    }
}
</code></pre>
