title: PHP Remove text before and after element
body: <p>I have some database rows with a lot of text but I only want to select a specific text.</p>
  
  <p>Example:</p>
  
  <pre><code>&lt;strong&gt;Lorem&lt;/strong&gt;
  &lt;ul&gt;
      &lt;li&gt;Lorem&lt;/li&gt;
      &lt;li&gt;Ipsum&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;strong&gt;Ipsum&lt;/strong&gt;
  &lt;ul&gt;
      &lt;li&gt;Ipsum&lt;/li&gt;
      &lt;li&gt;Lorem&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;strong&gt;Third&lt;/strong&gt;
  &lt;ul&gt;
      &lt;li&gt;Some words&lt;/li&gt;
      &lt;li&gt;More words&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I only want to select the Ipsum part with the ul element and ignore the rest. I was trying to work with <code>DOMXPath-&gt;evaluate('//text()[contains()]')</code> but this only shows the Ipsum text and not the elements.</p>
  
  <p>EDIT:
  The exact output I want is:</p>
  
  <pre><code>&lt;strong&gt;Ipsum&lt;/strong&gt;
  &lt;ul&gt;
      &lt;li&gt;Ipsum&lt;/li&gt;
      &lt;li&gt;Lorem&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The code I tried (I have tried more but this is the last result):</p>
  
  <pre><code>$d = new DOMDocument;
  $d-&gt;loadHTML($text);
  
  $x = new DOMXPath($d);
  $result = $x-&gt;evaluate("//text()[contains(., 'Ipsum')]");
  
  $result-&gt;item(0)-&gt;nodeValue;
  </code></pre>
  
  <p>This shows only the Ipsum text and not the list elements.</p>