如果您在字符串中有HTML服务器端,则可以使用DOMDocument:
<?php
$html = '<html><body><div id="first"></div><div id="second"></div></body></html>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$element = $dom->getElementById('second');
// this will be null if it isn't found
var_dump($element);
Run Code Online (Sandbox Code Playgroud)