title: DOM Manipulation with PHP
body: <p>I would like to make a simple but non trivial manipulation of DOM Elements with PHP but I am lost.</p>
  
  <p>Assume a page like Wikipedia where you have paragraphs and titles (<code>&lt;p&gt;</code>, <code>&lt;h2&gt;</code>). They are siblings. I would like to take both elements, in sequential order. </p>
  
  <p>I have tried <code>GetElementbyName</code> but then you have no possibility to organize information.
  I have tried <code>DOMXPath-&gt;query()</code> but I found it really confusing.</p>
  
  <p>Just parsing something like:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;&lt;/head&gt;
    &lt;body&gt;
      &lt;h2&gt;Title1&lt;/h2&gt;
      &lt;p&gt;Paragraph1&lt;/p&gt;
      &lt;p&gt;Paragraph2&lt;/p&gt;
      &lt;h2&gt;Title2&lt;/h2&gt;
      &lt;p&gt;Paragraph3&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>into:</p>
  
  <pre>
  Title1
  Paragraph1
  Paragraph2
  Title2
  Paragraph3
  </pre>
  
  <p>With a few bits of HTML code I do not need between all.</p>
  
  <p>Thank you. I hope question does not look like homework.</p>