あのときコード

PHPでのDOM操作

PHP DOM xpath DOMDocument DOMXPath

class="hoge"の要素をspanタグで囲む

$dom = new DOMDocument;

$html =  mb_convert_encoding($html, 'HTML-ENTITIES', 'auto');
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$nodes = $xpath->query('//div[contains(@class,"hoge")]');

foreach ($nodes as $node) {
    $span = $dom->createElement('span');
    $span->nodeValue = $node->nodeValue;
    $node->nodeValue = null;
    $node->appendChild($span);
}

return $dom->saveHTML();

参考サイト