2012-08-11 98 views
1

請原諒我,因爲我是一位新手程序員。我如何在PHP中將生成的$ matches(preg_match)值與第一個字符剝離到另一個變量($ funded)中?你可以看到我有什麼:如何將preg_match_all變量賦值給有價值的

<?php 
$content = file_get_contents("https://join.app.net"); 

//echo $content; 

preg_match_all ("/<div class=\"stat-number\">([^`]*?)<\/div>/", $content, $matches); 
//testing the array $matches 

//echo sprintf('<pre>%s</pre>', print_r($matches, true)); 

$funded = $matches[0][1]; 

echo substr($funded, 1); 
?> 
+0

我不確定你在問什麼。如果您提供一些樣本輸入和樣本所需輸出,它可能會有所幫助。 – Trott 2012-08-11 01:35:22

回答

0

Don't parse HTML with RegEx

最好的辦法是使用PHP DOM

<?php 
$handle = curl_init('https://join.app.net'); 
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
$raw = curl_exec($handle); 
curl_close($handle); 
$doc = new DOMDocument(); 
$doc->loadHTML($raw); 
$elems = $doc->getElementsByTagName('div'); 
foreach($elems as $item) { 
    if($item->getAttribute('class') == 'stat-number') 
     if(strpos($item->textContent, '$') !== false) $funded = $item->textContent; 
} 
// Remove $ sign and , 
$funded = preg_replace('/[^0-9]/', '', $funded); 
echo $funded; 
?> 

這在投寄時退還380950

0

我不是100%肯定,但它似乎是你試圖獲得美元金額目前的資金?

而角色是一個美元符號,你想剝離?

如果是這種情況,爲什麼不只是將美元符號添加到組外的正則表達式中,以免它被捕獲。

/<div class=\"stat-number\">\$([^`]*?)<\/div>/ 

因爲$意味着在正則表達式中的行尾,所以你必須先用斜槓轉義它。