更新时间:2023-02-20 09:03:17
参见 解析 HTML 的***方法.在下面找到 DOM 解决方案:
See Best methods to parse HTML. Find the DOM solution below:
$dom = new DOMDocument;
$dom->loadHTMLFile('http://example.com');
$options = array();
foreach($dom->getElementsByTagName('option') as $option) {
$options[$option->nodeValue] = $option->getAttribute('value');
}
这可以使用正则表达式完成 也一样,但我觉得用正则表达式编写可靠的 HTML 解析器是不切实际的,因为 PHP 有很多现成的本机和第 3 方解析器.
This can be done with Regex too, but I dont find it practical to write a reliable HTML parser with Regex when there is plenty of native and 3rd party parsers readily available for PHP.