PHP 字符串替换
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将字符串分解为数组:
这将为您提供:
然后,对该数组执行您想要的任何操作; 比如删除您不再想要的条目:
这会给您:
最后,将各个部分重新组合在一起:
您会得到您想要的:
也许不像正则表达式那么“简单”; 但是当你需要对你的元素做更多的事情(或者当你的元素比普通数字更复杂的那一天),这仍然有效:-)
编辑:如果你想删除“空”值,比如当有两个逗号时,您只需修改条件,有点像这样:
即排除空的
$values
。 使用“trim
”,因此也可以处理$string = "22,23, ,24,25";
,顺便说一句。YOu could explode the string into an array :
Which will give you :
Then, do whatever you want on that array ; like remove the entry you don't want anymore :
Which gives you :
And, finally, put the pieces back together :
And you get what you wanted :
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 :
ie, exclude the
$values
that are empty. The "trim
" is used so$string = "22,23, ,24,25";
can also be dealt with, btw.另一个问题是,如果您有一个用户 5 并尝试删除它们,您会将 15 变为 1,25 变为 2,等等。因此您必须检查两侧是否有逗号。
如果您想要这样的分隔字符串,我会在搜索和列表的两端都放一个逗号,但如果它变得很长,效率会很低。
一个例子是:
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:
类似于 Pascal 的选项,尽管我认为有点简单:
基本上,将字符串转换为数组,找到用户 ID,如果存在,则取消设置,然后再次内爆数组。
An option similar to Pascal's, although I think a bit simipler:
Basically, convert the string into an array, find the user ID, if it exists, unset it and then implode the array again.
我会采用简单的方法:在列表周围添加逗号,用单个逗号替换“,23,”,然后删除多余的逗号。 快速而简单。
话虽如此,操作逗号分隔列表中的值通常是糟糕设计的标志。 这些值应该位于数组中。
I would go the simple way: add commas around your list, replace ",23," with a single comma then remove extra commas. Fast and simple.
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.
尝试使用 preg:
更新:将
$pattern = '/$usrID,?/i';
更改为$pattern = '/' 。 $usrID 。 ',?/i';
更新2:更改了
$pattern = '/' 。 $usrID 。 ',?/i
到$pattern = '/\b' 。 $usrID 。 '\b,?/i'
来解决 onnodb 的评论...Try using preg:
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...简单的方法(提供所有 2 位数字):
Simple way (providing all 2 digit numbers):