<p data-attr-1="foo" data-attr-2="bar" >
$class_attr_node = $dom->createAttribute( 'class' );
$class_attr_node
первым атрибутом, чтобы получилось так:<p class="some-class" data-attr-1="foo" data-attr-2="bar" >
$node->attributes->prepend( $class_attr_node );
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$paragraphs = $xpath->query('/html/body/p');
foreach ($paragraphs as $paragraph) {
$attributes = $paragraph->attributes;
$first = $attributes->item(0);
$new_attr = $dom->createAttribute('class');
$new_attr->value = 'some-class';
$paragraph->insertBefore($new_attr, $first);
}
var_dump($dom->saveHTML());