elementChildren($wrapper); // Уровень 1: прямые дети обёртки одинаковы if (count($children) >= 2 && $this->allSimilar($children)) { return [$wrapper, $children]; } // Уровень 2: единственный дочерний элемент содержит похожих детей if (count($children) === 1) { $grand = $this->elementChildren($children[0]); if (count($grand) >= 2 && $this->allSimilar($grand)) { return [$children[0], $grand]; } } return null; } /** Все элементы имеют одинаковый тег и класс (считаем «похожими»). */ private function allSimilar(array $els): bool { $tag = strtolower($els[0]->tagName); $class = $els[0]->getAttribute('class'); foreach (array_slice($els, 1) as $el) { if (strtolower($el->tagName) !== $tag) return false; if ($el->getAttribute('class') !== $class) return false; } return true; } }