简体   繁体   中英

php regex get text between html tags

I want to scrap information about products from other site and the tags in which the price is held looks like that:

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

In this case I need to extract only 15.90 . I have tried this:

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

Where 'url' is the url from which I scrap the products, but when I check the $product_prices var with var_dump() it says NULL

Using Simple Dom Parser http://simplehtmldom.sourceforge.net/ seems the best idea for doing what you need.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM