如何从字符串中删除特定数字,如果该数字重复,我只需要将其删除一次。$string = "1 3 5 15 5";//Remove 5$valuetoremove = "5";$newvalue = "1 3 15 5";我试过 str_replace 但这会删除所有重复的数字。str_replace("5"," ","1 3 5 15 5");$newvalue = "1 3";
1 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
如果您特别想将其删除一次,那么我想这就是您所追求的
$string = "1 3 5 15 5";
$valueToRemove = "5";
$exploded = explode(" ", $string);
// $exploded = ["1", "3", "5", "15", "5"];
$key = array_search($valueToRemove, $exploded, true);
if ($key !== false) {
unset($exploded[$key]);
}
$newValue = implode(" ", $exploded);
- 1 回答
- 0 关注
- 132 浏览
添加回答
举报
0/150
提交
取消