Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从提取的数据中累加整数_Php_Html_Xpath_Web Scraping_Domdocument - Fatal编程技术网

Php 从提取的数据中累加整数

Php 从提取的数据中累加整数,php,html,xpath,web-scraping,domdocument,Php,Html,Xpath,Web Scraping,Domdocument,我有一个从网站上提取整数值的代码。我想知道我是否可以把所有这些整数相加,同时显示总和 <?php header('Content-Type: text/html; charset=utf-8'); $grep = new DoMDocument(); @$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp"); $finder = new DomXPath($grep); $class = "Cat

我有一个从网站上提取整数值的代码。我想知道我是否可以把所有这些整数相加,同时显示总和

<?php
header('Content-Type: text/html; charset=utf-8');
$grep = new DoMDocument();
@$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");

$finder = new DomXPath($grep);
$class = "CatLevel1";
$nodes = $finder->query("//*[contains(@class, '$class')]");

foreach ($nodes as $node) {
    $span = $node->childNodes;
    echo str_replace(array('(', ')'), '', $span->item(1)->nodeValue);
    echo '<br/>';
}    
?>

您只需将整数分配给变量,并在每次迭代
foreach

$total = 0;
foreach ($nodes as $node) {
    $span = $node->childNodes;
    echo str_replace(array('(', ')'), '', $span->item(1)->nodeValue);
    $total += $span->item(1)->nodeValue;
    echo '<br/>';
}
echo "Total: ".$total;
$total=0;
foreach($node作为$node){
$span=$node->childNodes;
echo str_替换(数组(“(”,“)”),“$span->item(1)->nodeValue);
$total+=$span->item(1)->nodeValue;
回声“
”; } echo“总计:”.$Total;

更新:确保您在
$span->item(1)->nodeValue中得到的是整数而不是字符串。您可以修改
$total+=$span->item(1)->nodeValue
$total+=intval($span->item(1)->nodeValue)
要将返回字符串(如“97”)转换为
int
97。

只需将其像普通变量一样相加即可。在顶部初始化零。例如:

$total = 0;
foreach ($nodes as $node) {
    $span = $node->childNodes;
    $number = preg_replace("/[^0-9]/", '', $span->item(1)->nodeValue);
    echo '<br/>';

    $total += (int) $number;
}

echo "Total: $total";
$total=0;
foreach($node作为$node){
$span=$node->childNodes;
$number=preg_replace(“/[^0-9]/”,“”,$span->item(1)->nodeValue);
回声“
”; $total+=(int)$number; } echo“总计:$Total”;
看起来您的代码完成了您想要完成的90%,相比之下,整数的添加应该相对简单

假设此部分返回整数:

foreach ($nodes as $node) {
    $span = $node->childNodes;
    echo str_replace(array('(', ')'), '', $span->item(1)->nodeValue);
    echo '<br/>';
} 
foreach($nodes作为$node){
$span=$node->childNodes;
echo str_替换(数组(“(”,“)”),“$span->item(1)->nodeValue);
回声“
”; }
将其更改为:

$total = 0;
foreach ($nodes as $node) {
    $span = $node->childNodes;
    $integer = str_replace(array('(', ')'), '', $span->item(1)->nodeValue);
    $total += $integer;
    echo "$integer<br/>";
} 
echo "Total: $total";
$total=0;
foreach($node作为$node){
$span=$node->childNodes;
$integer=str_replace(数组(“(”,“)”),“$span->item(1)->nodeValue);
$total+=$integer;
回显“$integer
”; } echo“总计:$Total”;