php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49835 Counting and removing multiple attributes doesn't work
Submitted: 2009-10-10 23:55 UTC Modified: 2009-10-11 09:04 UTC
From: vtap at club-internet dot fr Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3.0 OS: Windows 7 RTM
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vtap at club-internet dot fr
New email:
PHP Version: OS:

 

 [2009-10-10 23:55 UTC] vtap at club-internet dot fr
Description:
------------
Trying to remove all attributes of the <body> tag in an HTML file. 

1/ Just counting : count is always equal to 1 whatever the real number of attributes is (varying from 0 to 3) though the name function retrieves all entries
2/ Removing : count is always equal to 1, only the first attribute is removed and breaks the loop, showing only the first one. Tried removeAttribute and removeAttributenode


Reproduce code:
---------------
$html = new DOMDocument();
$html -> loadHTMLFile("mypath\\myfile.html");
//Recherche du body et suppression de tous ses attributs
$xpath = new DOMXPath($html);
$query = "//body";
foreach ($xpath -> query($query) as $node) {
    echo "Count before = " .
        count($node-> attributes) .
        "<br />";
    foreach ($node-> attributes as $attribute) {
        //                $node-> removeAttributenode($attribute);
        echo "$attribute->name <br />";
        //            $node-> removeAttribute($attribute -> name);
    }
    echo "Count after = " .
        count($node-> attributes) .
        "<br />";
}


Expected result:
----------------
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
displays (just counting)
Count before = 1
bgcolor
text
Count after= 1

displays (removing)

Count before = 1
bgcolor
Count after= 1

and gives
<body text="#000000">


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-11 07:03 UTC] vtap at club-internet dot fr
Found a bypass solution :

    $query = "//body";
    foreach ($xpath -> query($query) as $element) {
        $names = array();
        foreach ($element -> attributes as $attribute) {
            $names[] = $attribute -> name;
        }
        foreach ($names as $name) {
            $element -> removeAttribute($name);
        }
    }

ugly but it works...
 [2009-10-11 09:04 UTC] Sjoerd@php.net
Thank you for your bug report.

DOMXPath->query does not return an array, but a DOMNodeList. The count() function returns the number of elements in an array, but a DOMNodeList is not an array. Use DOMNodeList->length instead.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 11:01:32 2025 UTC