Answer the question
In order to leave comments, you need to log in
PHP. What is the magic?
Good afternoon,
I've encountered a small problem today. I managed to solve it, but I didn’t understand one thing:
In short, here is a working piece of code
foreach ($this->allowableTags as $tag) {
foreach ($doc->getElementsByTagName($tag) as $element) {
$attributes = $element->attributes;
$attrs = [];
for ($i = 0; $i < $attributes->length; ++$i) {
$attrs[] = $attributes->item($i);
}
foreach ($attrs as $attr) {
$this->clearElement($element, $attr);
}
}
}
foreach ($this->allowableTags as $tag) {
foreach ($doc->getElementsByTagName($tag) as $element) {
$attributes = $element->attributes;
for ($i = 0; $i < $attributes->length; ++$i) {
$attr = $attributes->item($i);
$this->clearElement($element, $attr);
}
}
}
Answer the question
In order to leave comments, you need to log in
Because when an attribute is removed, the rest are renumbered. Let there be three attributes, their numbers will be 0, 1 and 2.
Шаг 1.
$i = 0.
Удаляем атрибут 0.
Атрибуты 1 и 2 получают номера 0 и 1.
Шаг 2.
$i = 1.
Удаляем атрибут 1.
Остался атрибут 0.
foreach ($this->allowableTags as $tag) {
foreach ($doc->getElementsByTagName($tag) as $element) {
$attributes = $element->attributes;
while ($attributes->length > 0) {
$attr = $attributes->item(0);
$this->clearElement($element, $attr);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question