Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:40:41+00:00 2026-06-01T02:40:41+00:00

I use DOMXPATH to remove all attributes from the p tag and it works

  • 0

I use DOMXPATH to remove all attributes from the p tag and it works fine,

// 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 );
    }

}

And now I want to remove style attribute only from the p tag.

// 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" ) );
    }

}

But I have this error in return,

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

How can I remove style attribute only?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:40:43+00:00Added an answer on June 1, 2026 at 2:40 am

    replace this

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

    with something like this:

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

    (not tested, sorry, i don’t have a server installed here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use DOMxpath to remove html tags that have empty text node but to
We use a data acquisition card to take readings from a device that increases
I know that I can use DOMDocument and DOMXPath to manipulate XML files. But,
I'm trying to map a list of fields from a website using php DOMXPath
I build a script that combines all css on a page together to use
i am using DOM to get content of div tag but inner html part
I have the following script that works almost fine except two things: I still
I am getting some info from an RSS. <?php $dom = new DOMDocument; libxml_use_internal_errors(TRUE);
I use this code: I'm trying to remove the categoryPath node but keep its
Is there any way to use DOMXPath in C# like PHP for web scraping?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.