两个小时前,以下脚本有效。现在由于某种原因,我收到一个错误“警告:第 30 行的 _ 中除以零”。这是脚本。谁能告诉我我做错了什么以及如何纠正它?
基本上,这个脚本是从另一个网站上的两个元素中提取数据,将它们分开以获得一个数字,该数字又用于设置元素的宽度。对此进行了一段时间的故障排除。提前非常感谢!
<?php
define("FFF_SIXDEGREES", "http://www.stayclassy.org/fundraise?fcid=257739");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FFF_SIXDEGREES);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(!($results = curl_exec($curl))) {
print("{ \"total\": \"$0.00\" }");
return;
}
$pattern = '/<li class="goalTitle">Raised so far:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern, $results, $matches);
$total = $matches[1];
$total = str_replace(",", "", $total);
// printf("<h2 class=\"raised-total\">$%s</h2>", formatMoney($total, true));
$pattern2 = '/<li class="goalTitle">My goal:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern2, $results, $matches);
$total2 = $matches[1];
$total2 = str_replace(",", "", $total2);
// printf("<h2 class=\"goal-total\">$%s</h2>", formatMoney($total2, true));
$diff = ($total/$total2) * 100; // THIS IS THE LINE OF CODE IN QUESTION
function formatMoney($number, $fractional=false)
{
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
echo "<div class=\"progress-header\" style=\"width:$diff%;\"><span class=\"raised-amount\">$$total</span><span class=\"goal-amount\">$$total2</span></div>";
?>