2013-02-17 234 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" }