Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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 str_replace";数组";没有删除逗号_Php - Fatal编程技术网

Php str_replace";数组";没有删除逗号

Php str_replace";数组";没有删除逗号,php,Php,我试图删除百分比(%)和逗号(,),它似乎只删除了百分比 function votesremaining($totalvotes, $firstplace, $currentplace) { $search = array('%', ','); $replace = array('', ''); $formula = round(str_replace($search,$replace,$totalvotes * ((($firstplace+1) - $currentp

我试图删除百分比(%)和逗号(,),它似乎只删除了百分比

function votesremaining($totalvotes, $firstplace, $currentplace) {
    $search = array('%', ',');
    $replace = array('', '');
    $formula = round(str_replace($search,$replace,$totalvotes * ((($firstplace+1) - $currentplace) / 100)));
    return $formula;
}

我有什么地方做错了吗?

你可能把括号搞错了。一人在投票后失踪

$formula = round(str_replace($search,$replace,$totalvotes) * ((($firstplace+1) - $currentplace) / 100));
然而,我不太确定你对这一说法的期望。该产品可能无论如何都不会工作

也许你的意思是将字符串拆分成一个数组,然后将每个条目乘以这里的乘积


更新:


我看到您发布了一条评论,几乎是说,
$totalvowers
将只是一个数字,但可能带有逗号和百分比字符。所以确实,不需要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;

}

介意展示一些输入的样子吗?(即,
$totalvoates,$firstplace,$currentplace
)是totalvoates、firstvoates和currentplace数字吗?你为什么要用%或,?它们似乎是整数读取输入作为$firstplace和$currentplace的百分比输入$totalvotes通常用逗号输入,我需要去掉逗号。