The link you posted has the answer:
// Create DOM from string
$html = str_get_html('Hello
World
');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: foo
World
Of course, you will need to modify the tag/attribute/value names to meet your specific needs.