title: PHP remove all html tags and keep plain text with DOM Parser
body: <p>I am trying to find the best way to strip all the html tags from a string to keep only plain text. Now the easiest way ofcourse would be <code>strip_tags</code>, however it is not optimal since it will not work well with broken tags and etc.. I think that what i need is a DOM Parser. However I have no clue how this thing works.</p>
  
  <p>For example i have a simple string:</p>
  
  <pre><code>    &lt;p&gt;
          &lt;strong&gt;​
              Some plain text
          &lt;/strong&gt;
      &lt;/p&gt;
  </code></pre>
  
  <p>I want to strip all the tags with DOM parser and keep the plain text:</p>
  
  <pre><code>Some plain text
  </code></pre>
  
  <p>How can i do that? I tried to use removeChild but it removes everything even the text:</p>
  
  <pre><code>$dom = new DOMDocument();
  $dom-&gt;loadHTML($translation-&gt;text);
  
  foreach ($dom-&gt;getElementsByTagName("*") as $tag) {
      $tag-&gt;parentNode-&gt;removeChild($tag);
  };
  </code></pre>