2012-08-11 24 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>/ 

因为$意味着在正则表达式中的行尾,所以你必须先用斜杠转义它。