我有一个代码可以检索整数值,然后将它们合计。但是,当我尝试向提取的值添加千位分隔符时,它将无法正确计算。
这是代码:
<?php
$ch = curl_init('http://www.alibaba.com/Products');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$html = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$finder = new DOMXPath($dom);
$nodes = $finder->query('//h4[@class="sub-title"]');
$total_A = 0;
foreach ($nodes as $node) {
$sub_no = (int) preg_replace("/[^0-9]/", '', trim(explode("\n", trim($node->nodeValue))[2])) . '<br/>';
$total_A += $sub_no;
echo $sub_no;
$convert = number_format( $total_A , 0 , '.' , ',' );
}
echo "Total: $convert";
?>
我试过了:
$sub_no = number_format(preg_replace("/[^0-9]/", '', trim(explode("\n", trim($node->nodeValue))[2])), 0 , '.' , ',' ) . '<br/>';
并且它适用于提取的值,但总数将被搞砸。我怎么能纠正这个?谢谢!
编辑:我设法得到了正确的输出:407,418,309,但我也想要 当我回应它时,$ sub_no也有千位分隔符(不仅仅是总数)。但是当我这样做时,总数将被错误地显示出来。 输出应该是这样的:答案 0 :(得分:4)
为什么不将最终输出回显为number_format()
?
echo "Total: ".number_format($convert, 0 , '.' , ',' );
或者更确切地说:
$total_A = 0;
foreach ($nodes as $node) {
$sub_no = (int) preg_replace("/[^0-9]/", '', trim(explode("\n", trim($node->nodeValue))[2])) . '<br/>';
$total_A += $sub_no;
echo $sub_no;
}
echo "Total: ".number_format($total_A, 0 , '.' , ',' );
答案 1 :(得分:1)
您可以先将所有变量合计,然后将它们格式化,以便查看。这里的简单逻辑是先将算术分开,然后在算术后对它们进行格式化,这样就不会发生冲突。
$total_A = 0;
foreach ($nodes as $node) {
$sub_no = (int) preg_replace("/[^0-9]/", '', trim(explode("\n", trim($node->nodeValue))[2]));
$total_A += $sub_no;
echo number_format($sub_no, 0 , '.' , ',' ) . '<br/>';
}
echo "Total: ".number_format($total_A, 0 , '.' , ',' );