PHP 字符串替换

发布于 2024-07-29 20:21:54 字数 370 浏览 6 评论 0原文

我目前正在使用 str_replace 删除 usrID 和其后的“逗号”:

例如:

$usrID = 23;
$string = "22,23,24,25";
$receivers = str_replace($usrID.",", '', $string);  //Would output: "22,24,25"

但是,我注意到 if:

$usrID = 25; //or the Last Number in the $string

它不起作用,因为“25”后没有尾随“逗号”

有没有更好的方法可以从字符串中删除特定数字?

谢谢。

I'm currently using str_replace to remove a usrID and the 'comma' immediately after it:

For example:

$usrID = 23;
$string = "22,23,24,25";
$receivers = str_replace($usrID.",", '', $string);  //Would output: "22,24,25"

However, I've noticed that if:

$usrID = 25; //or the Last Number in the $string

It does not work, because there is not a trailing 'comma' after the '25'

Is there a better way I can be removing a specific number from the string?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白昼 2024-08-05 20:21:54

您可以将字符串分解为数组:

$list = explode(',', $string);
var_dump($list);

这将为您提供:

array
  0 => string '22' (length=2)
  1 => string '23' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

然后,对该数组执行您想要的任何操作; 比如删除您不再想要的条目:

foreach ($list as $key => $value) {
    if ($value == $usrID) {
        unset($list[$key]);
    }
}
var_dump($list);

这会给您:

array
  0 => string '22' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

最后,将各个部分重新组合在一起:

$new_string = implode(',', $list);
var_dump($new_string);

您会得到您想要的:

string '22,24,25' (length=8)

也许不像正则表达式那么“简单”; 但是当你需要对你的元素做更多的事情(或者当你的元素比普通数字更复杂的那一天),这仍然有效:-)


编辑:如果你想删除“空”值,比如当有两个逗号时,您只需修改条件,有点像这样:

foreach ($list as $key => $value) {
    if ($value == $usrID || trim($value)==='') {
        unset($list[$key]);
    }
}

即排除空的 $values 。 使用“trim”,因此也可以处理$string = "22,23, ,24,25";,顺便说一句。

YOu could explode the string into an array :

$list = explode(',', $string);
var_dump($list);

Which will give you :

array
  0 => string '22' (length=2)
  1 => string '23' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

Then, do whatever you want on that array ; like remove the entry you don't want anymore :

foreach ($list as $key => $value) {
    if ($value == $usrID) {
        unset($list[$key]);
    }
}
var_dump($list);

Which gives you :

array
  0 => string '22' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

And, finally, put the pieces back together :

$new_string = implode(',', $list);
var_dump($new_string);

And you get what you wanted :

string '22,24,25' (length=8)

Maybe not as "simple" as a regex ; but the day you'll need to do more with your elements (or the day your elements are more complicated than just plain numbers), that'll still work :-)


EDIT : and if you want to remove "empty" values, like when there are two comma, you just have to modifiy the condition, a bit like this :

foreach ($list as $key => $value) {
    if ($value == $usrID || trim($value)==='') {
        unset($list[$key]);
    }
}

ie, exclude the $values that are empty. The "trim" is used so $string = "22,23, ,24,25"; can also be dealt with, btw.

无人问我粥可暖 2024-08-05 20:21:54

另一个问题是,如果您有一个用户 5 并尝试删除它们,您会将 15 变为 1,25 变为 2,等等。因此您必须检查两侧是否有逗号。

如果您想要这样的分隔字符串,我会在搜索和列表的两端都放一个逗号,但如果它变得很长,效率会很低。

一个例子是:

$receivers = substr(str_replace(','.$usrID.',', ',', ','.$string.','),1,-1);

Another issue is if you have a user 5 and try to remove them, you'd turn 15 into 1, 25 into 2, etc. So you'd have to check for a comma on both sides.

If you want to have a delimited string like that, I'd put a comma on both ends of both the search and the list, though it'd be inefficient if it gets very long.

An example would be:

$receivers = substr(str_replace(','.$usrID.',', ',', ','.$string.','),1,-1);
盛夏已如深秋| 2024-08-05 20:21:54

类似于 Pascal 的选项,尽管我认为有点简单:

$usrID = 23;
$string = "22,23,24,25";
$list = explode(',', $string);
$foundKey = array_search($usrID, $list);
if ($foundKey !== false) {
    // the user id has been found, so remove it and implode the string
    unset($list[$foundKey]);
    $receivers = implode(',', $list);
} else {
    // the user id was not found, so the original string is complete
    $receivers = $string;
}

基本上,将字符串转换为数组,找到用户 ID,如果存在,则取消设置,然后再次内爆数组。

An option similar to Pascal's, although I think a bit simipler:

$usrID = 23;
$string = "22,23,24,25";
$list = explode(',', $string);
$foundKey = array_search($usrID, $list);
if ($foundKey !== false) {
    // the user id has been found, so remove it and implode the string
    unset($list[$foundKey]);
    $receivers = implode(',', $list);
} else {
    // the user id was not found, so the original string is complete
    $receivers = $string;
}

Basically, convert the string into an array, find the user ID, if it exists, unset it and then implode the array again.

万水千山粽是情ミ 2024-08-05 20:21:54

我会采用简单的方法:在列表周围添加逗号,用单个逗号替换“,23,”,然后删除多余的逗号。 快速而简单。

$usrID = 23;
$string = "22,23,24,25";
$receivers = trim(str_replace(",$usrID,", ',', ",$string,"), ',');

话虽如此,操作逗号分隔列表中的值通常是糟糕设计的标志。 这些值应该位于数组中。

I would go the simple way: add commas around your list, replace ",23," with a single comma then remove extra commas. Fast and simple.

$usrID = 23;
$string = "22,23,24,25";
$receivers = trim(str_replace(",$usrID,", ',', ",$string,"), ',');

With that said, manipulating values in a comma separated list is usually sign of a bad design. Those values should be in an array instead.

煞人兵器 2024-08-05 20:21:54

尝试使用 preg:

<?php
$string = "22,23,24,25";
$usrID = '23';
$pattern = '/\b' . $usrID . '\b,?/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

更新:将 $pattern = '/$usrID,?/i'; 更改为 $pattern = '/' 。 $usrID 。 ',?/i';
更新2:更改了 $pattern = '/' 。 $usrID 。 ',?/i$pattern = '/\b' 。 $usrID 。 '\b,?/i' 来解决 onnodb 的评论...

Try using preg:

<?php
$string = "22,23,24,25";
$usrID = '23';
$pattern = '/\b' . $usrID . '\b,?/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

Update: changed $pattern = '/$usrID,?/i'; to $pattern = '/' . $usrID . ',?/i';
Update2: changed $pattern = '/' . $usrID . ',?/i to $pattern = '/\b' . $usrID . '\b,?/i' to address onnodb's comment...

哎呦我呸! 2024-08-05 20:21:54

简单的方法(提供所有 2 位数字):

$string = str_replace($userId, ',', $string);
$string = str_replace(',,','', $string);

Simple way (providing all 2 digit numbers):

$string = str_replace($userId, ',', $string);
$string = str_replace(',,','', $string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文