$dom = new DOMDocument; $dom->loadHTML($html); $divs = $dom->getElementsByTagName('div'); foreach ($divs as $div) { $div->setAttribute('class', $div->getAttribute('class').' myclass'); } $html = $dom->saveHTML();