2013-02-17 230 views
1

我有一个字符串,从一个file_get_contents()函数调用,包含如下:提取使用的preg_match价格在PHP

<span class="cb_price_countdown cb_lot_price_1439066">$40.65</span> 

而且我想提取物价格40.65。我不熟悉正则表达式或preg_match,所以我很难。到目前为止,我曾尝试:

$pattern = "/\\\$?((\d{1,3}(,\d{3})*)|(\d+))(\.\d{2})?$/"; 
    preg_match ($pattern, $subject, $matches); 
    print_r ($matches); 

这是不返回任何有用,我已经试过:

​​

,但它是相同的故事。有人能告诉我正在寻找正确的preg_match模式吗?

谢谢
贾斯汀

+0

不使用正则表达式这一点。不要从其他网站窃取内容。 – 2013-02-17 19:34:13

+2

[不要使用正则表达式解析HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – 2013-02-17 19:34:36

+0

我应该怎么做使用? – JuJoDi 2013-02-17 19:34:40

回答

5

使用本:

preg_match ('/\$(\d+\.\d+)/', $subject, $matches); 
+0

这回答了我的问题,谢谢! – JuJoDi 2013-02-17 19:40:28

+0

因此,您可以将我的帖子标记为答案!谢谢 :) – Boynux 2013-02-17 19:41:16

1
$result = array(); 
$str = '<span class="cb_price_countdown cb_lot_price_1439066">$40.65</span>'; 
preg_match('/<span[^>]*>\$(.*)<\/span>/', $str, $result); 
var_dump($result); 

产量:

array(2) { [0]=> string(61) "$40.65<" [1]=> string(5) "40.65" }