php regex在html标签之间获取文本

时间:2017-02-28 14:26:14

标签: php regex web-crawler

我想废弃其他网站上的产品信息以及价格所持有的标签:

<span class="text10black">Price: <strong style="color:#000000;">15.90 $</strong></span>

在这种情况下,我只需要提取 15.90 。 我试过这个:

$site_content = file_get_contents('url');
preg_match_all('#<span class="text10black">Price: <strong style="color:#000000;">(.*?) $</strong></span>#', $site_content, $product_prices);

其中'url'是我废弃产品的网址,但是当我用var_dump()检查$ product_prices var时,它表示NULL

1 个答案:

答案 0 :(得分:0)

使用Simple Dom Parser http://simplehtmldom.sourceforge.net/似乎是做你需要的最好的主意。

$html = file_get_html($url);
foreach($html->find('.text10black strong') as $element)
    var_dump($element->plaintext);