1

请原谅我,因为我是一个新手程序员。如何将结果 $matches (preg_match) 值(去掉第一个字符)分配给 php 中的另一个变量 ($funded)?您可以在下面看到我的内容:

<?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);
?>
4

2 回答 2

0

我不是 100% 确定,但您似乎正试图获得资金目前的金额?

并且字符是您要删除的美元符号?

如果是这种情况,为什么不将美元符号添加到组外的正则表达式中,这样它就不会被捕获。

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

因为 $ 表示正则表达式中的行尾,所以您必须首先用斜杠对其进行转义。

于 2012-08-11T01:43:07.780 回答
0

不要使用 RegEx 解析 HTML

最好的方法是使用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在发布时返回。

于 2012-08-11T03:15:18.063 回答