|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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">
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 01:00:01 2025 UTC |
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...