2012-03-27 5 views
2

Ich benutze DOMXPath allattributes vom p-Tag zu entfernen, und es funktioniert gut,Wie style Attribut nur mit DOMXPATH entfernen?

// Loop all p. 
foreach($dom->getElementsByTagName("p") as $p) 
{ 
    // Loop all attributes in p. 
    foreach($p->attributes as $attrib) 
    { 
      // Remove all attribute from p. 
      $p->removeAttributeNode($attrib); 
    } 

} 

Und jetzt will ich Stilattribute nur aus dem p-Tag entfernen.

// Loop all p. 
foreach($dom->getElementsByTagName("p") as $p) 
{ 
    // Loop all attributes in p. 
    foreach($p->attributes as $attrib) 
    { 
      // Remove only the style attribute 
     $p->removeAttributeNode($p->getAttributeNode("style")); 
    } 

} 

Aber ich habe diesen Fehler im Gegenzug

Catchable fatal error: Argument 1 passed to DOMElement::removeAttributeNode() must be an instance of DOMAttr, boolean given..

Wie kann ich Stilattribute nur entfernen?

Antwort

3

diese wie folgt

// Loop all attributes in p. 
foreach($p->attributes as $attrib) 
{ 
     // Remove only the style attribute 
    $p->removeAttributeNode($p->getAttributeNode("style")); 
} 

mit etwas ersetzen:

// fetch style node 
$sNode = $p->getAttributeNode("style") 
// only procede, if $p actually has a style node 
if ($sNode) { 
    $p->removeAttributeNode($sNode); 
} 

(nicht getestet, sorry, ich habe keinen Server hier installiert)

+0

Ihnen sehr danken. Es klappt! :-) – laukok