I have a code that retrieve the integer values and then totalling them. However, when I attempt to add thousand separator to the extracted value, it will not totalled correctly.
This is the code:
<?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("
", trim($node->nodeValue))[2])) . '<br/>';
$total_A += $sub_no;
echo $sub_no;
$convert = number_format( $total_A , 0 , '.' , ',' );
}
echo "Total: $convert";
?>
I tried :
$sub_no = number_format(preg_replace("/[^0-9]/", '', trim(explode("
", trim($node->nodeValue))[2])), 0 , '.' , ',' ) . '<br/>';
and it works for the extracted value but the total will be messed up. How can I correct this? Thanks!
EDIT: I managed to get the correct output for the total: 407,418,309 but I also want the
$sub_no to have thousand seperator too when I echo it(not just the total). But when I do it, the total will be displayed wrongly.
Output should be like this:
397,893,1 #notice the separator?
184,471,0
729,391,9
etc...
Total: 407,418,309