使用 XPath
$html = '<html><body><span class="text">Hello, World!</span></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$span = $xpath->query("//span[@class='text']")->item(0);
echo $span->textContent;
輸出:
Hello, World!