0

我正在寻找一个库、类、函数等,我可以向它提供一个 html 字符串并找到诸如 src 或 href 之类的东西。我知道我可以为特定情况找到一个正则表达式,但我正在寻找一个可以轻松获取东西而无需每次都找出新的正则表达式的库。

4

2 回答 2

1

在这种情况下, SimpleHTMLDOM是您的朋友。REGEX 从来都不是最好的方法(对于 HTML 标签)。

例子:

$html = file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('img') as $element) {
       echo $element->src . '<br>'; # givs you the image 'src' attribute for each image on that page.
}
于 2012-09-14T13:26:00.810 回答
1

假设您使用的是 PHP5,它已经在 DOMDocument 类中提供给您:

http://docs.php.net/manual/en/domdocument.loadhtml.php

于 2012-09-14T13:47:58.983 回答