title: PHP DOMDocument hasAttribute is associated to which Elements?
body: <p>I want to check all the tags under the body and check and remove if it has style attribute 
  I have tried</p>
  
  <pre><code>$user_submitted_html = "This is Some Text";
  $html = '&lt;body&gt;' . $user_submitted_html . '&lt;/body&gt;';
  
  $dom = new DOMDocument();
  $dom-&gt;loadHTML($html_string);
  $elements = $dom-&gt;getElementsByTagName('body');
  foreach($elements as $element) {
  
     foreach($element-&gt;childNodes as $child) {
  
        if($child-&gt;hasAttribute('style')) {
  
            $child-&gt;removeAttribute('style')
  
        }      
     }  
   }
  </code></pre>
  
  <p>It works fine if <code>$user_submitted_html</code> is not only text, mean if it has some tags in it, but If it is only text then It gives the error</p>
  
  <pre><code>Call to undefined method DOMText::hasAttribute()
  </code></pre>
  
  <p>Then I get the nodeName in the foreach loop</p>
  
  <pre><code>echo "Node Name: " . $child-&gt;nodeName
  </code></pre>
  
  <p>It gives the </p>
  
  <pre><code>Node Name = #text
  </code></pre>
  
  <p>What kind of node name is this, I have echo'ed other nodes, it gives, div, span etc. that I am familiar with. 
  I want to know that which are the elements that hasAttribute does not belong to them so I can put a condition before using the hasAttribute like this</p>
  
  <pre><code>if($child-&gt;nodeName=="#text") {
      continue; // skip to next iteration
  }
  if($child-&gt;hasAttribute('style')) {
  .
  .
  .
  </code></pre>
  
  <p>OR any Other Solution???</p>
  
  <p>One More Suggestion Required. What If I remove only the style attributes from <code>&lt;div&gt;,&lt;span&gt;,&lt;p&gt; and &lt;a&gt;</code>. Will it be safe from xss, if the rest of the tags can use style attribute. </p>