DOM manipulation library for PHP.
composer require blockify/dom
First, require Composer's autoloader:
require_once __DIR__ . '/vendor/autoload.php';
Then, use the library:
use Blockify\Dom\DOM;
$html = '<h1>Hello, World!</h1>';
$dom = DOM::parse( $html );
$h1 = $dom->getElementsByTagName('h1')[0];
$h1->setAttribute('class', 'heading');
echo $dom->saveHTML();