我试图删除百分比(%)和逗号(,),它似乎只删除百分比。
function votesremaining($totalvotes, $firstplace, $currentplace) {
$search = array('%', ',');
$replace = array('', '');
$formula = round(str_replace($search,$replace,$totalvotes * ((($firstplace+1) - $currentplace) / 100)));
return $formula;
}
我做错了什么吗?
你可能把括号弄乱了。$totalvotes后面少了一个
$formula = round(str_replace($search,$replace,$totalvotes) * ((($firstplace+1) - $currentplace) / 100));
然而,我不太确定你对这句话的期望是什么。这个产品很可能不管用。
也许你想在数组中拆分字符串,然后将每个条目乘以这里的乘积?
http://us2.php.net/manual/en/function.preg-split.php更新:
我看到你发表了一个评论,说$totalvotes
将只是一个数字,但可能有逗号和百分比字符。所以…实际上,不需要preg_split() !
您在进行数学运算之后进行替换-正常的用例是在进行数学运算之前进行替换。
我真的不知道你的函数的输入是什么,但它应该是这样的:
function votesremaining($totalvotes, $firstplace, $currentplace) {
$search = array('%', ',');
$replace = array('', '');
$totalvotes = str_replace($search, $replace, $totalvotes);
$firstplace = str_replace($search, $replace, $firstplace);
$currentplace = str_replace($search, $replace, $currentplace);
$formula = round($totalvotes * (($firstplace+1) - $currentplace) / 100);
return $formula;
}