将整数转换为千分隔符时遇到格式不正确的数值


A non well formed numeric value encountered when converting integer to thousand separator

我有一个从网站检索值的PHP代码。我得到了输出,但也收到了通知:注意:在第19行遇到一个格式不正确的数值。

这是我的PHP代码:

<?php
header('Content-Type: text/html; charset=utf-8');
$grep = new DoMDocument();
@$grep->loadHTMLFile("https://www.postme.com.my/men-1.html");
$finder = new DomXPath($grep);
$class = "amount";
$nodes = $finder->query("//*[contains(@class, '$class')]");
$i = 0;
  foreach ($nodes as $node) {
    if ($i<1) {
    $span = $node->childNodes;
    $replace = str_replace("Items 1-12 of", "",$span->item(0)->nodeValue);
    echo number_format($replace, 0 , '.' , ',' ) . '<br/>';
  }
   $i++;
  }
?>

我该如何更正它,以便在我为其指定数字格式时不会出现通知。谢谢

您可以在命令前面放置一个at符号,以忽略所有通知等。这通常是不好的形式,但它应该工作。

echo @number_format($replace, 0 , '.' , ',' ) . '<br/>';

编辑:@在错误的地方。

此外,正如建议的那样,您也可以检查以确保该值是数字的:

http://php.net/manual/en/function.is-numeric.php

if(is_numeric($replace){
    echo number_format($replace, 0 , '.' , ',' ) . '<br/>';
}

编辑编辑:您也可以尝试键入这里提到的变量:

number_format()导致Apache日志中出现错误