在php中使用正则表达式从html页面获取数据
我试图使用正则表达式获取给定页面上的价格,但用于存储获取内容的变量始终为空。有人能帮我写出正确的正则表达式吗 如果页面为: 我想在这里卖260英镑 标签页的一些html代码:在php中使用正则表达式从html页面获取数据,php,regex,fetch,Php,Regex,Fetch,我试图使用正则表达式获取给定页面上的价格,但用于存储获取内容的变量始终为空。有人能帮我写出正确的正则表达式吗 如果页面为: 我想在这里卖260英镑 标签页的一些html代码: <span id="fk-mprod-our-id" class="price final-price our fksk-our">Rs.<span class="small-font"> </span>260</span> 260卢比 看起来这是唯一一次使用最终价格类,所
<span id="fk-mprod-our-id" class="price final-price our fksk-our">Rs.<span class="small-font"> </span>260</span>
260卢比
看起来这是唯一一次使用最终价格
类,所以这应该是可行的:
/final-price.+?>(\d+)</
/final price.+?>(\d+)假设货币可能根据IP/国家而变化,我会使用Explode(我不太擅长正则表达式)
//考虑$html包含页面源代码
$html=分解(“”,$html);
$html=explode(“,$html[1]);
$price=$html[1];
我希望这会有所帮助。您可以使用simplehtmldom编写更多防弹解析器-请参阅。对我来说,它从未失败过解析文档
您将得到这样的代码
<?php
include_once '/path/to/simplehtmldom/simple_html_dom.php';
$html = file_get_html('http://www.flipkart.com/mobiles/memory-cards/itmczcsrtvjeb6nr?pid=acccrrqzzsgnfgea&_l=sXQjsX87GxqrvKzhjuOrkw--&_r=n_2yuAC4xgh0SZTuulvAtw--&ref=af8ad0c4-62a2-4381-99d3-3ad8285e260b');
foreach ($html->find('span.final-price') as $element) {
echo $element->plaintext;
}
//will output "Rs. 260", unless page changes
<?php
include_once '/path/to/simplehtmldom/simple_html_dom.php';
$html = file_get_html('http://www.flipkart.com/mobiles/memory-cards/itmczcsrtvjeb6nr?pid=acccrrqzzsgnfgea&_l=sXQjsX87GxqrvKzhjuOrkw--&_r=n_2yuAC4xgh0SZTuulvAtw--&ref=af8ad0c4-62a2-4381-99d3-3ad8285e260b');
foreach ($html->find('span.final-price') as $element) {
echo $element->plaintext;
}
//will output "Rs. 260", unless page changes