如何在爆炸和替换过程中删除逗号空格?
$data = "google,facebook,youtube,twitter,bing";
$exp = explode(",",$data);
$rep = str_replace("facebook",$exp);
$final = implode(",",$rep);
echo $final
output// google,,youtube,twitter,bing
如何用逗号删除这个空格?
$data = "google,facebook,youtube,twitter,bing";
$exp = explode(",",$data);
$rep = str_replace("facebook",$exp);
$final = implode(",",$rep);
echo $final
output// google,,youtube,twitter,bing
How can I remove this blank space with comma?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的代码应如下所示:
或者只要您的角点后没有空格,您就可以简单地使用
str_replace
来解决许多复杂问题>,就用loopy方法吧。Here's what your code should look like:
or as long as you have no spaces within after your comers you can simply goTo many complications using the
str_replace
, just use the loopy method.http://php.net/array-search
http://php.net/array-search
您可以使用
array_filter($data)
从数组中删除空元素:http://php.net/manual/en/function.array-filter.php
"如果未提供回调,则所有等于 FALSE 的输入条目(请参阅转换为布尔值)将被删除。 ”
这就是您要找的吗?
You can remove empty elements from an array using
array_filter($data)
:http://php.net/manual/en/function.array-filter.php
"If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed."
Is that what you're looking for?
有很多方法可以做到这一点,但也许最简单的就是:
There are many ways to do this but perhaps the simplest is just:
或者:
参见 preg_grep()
Alternatively:
See preg_grep()
罗伯特皮特的答案是唯一一个费心循环的答案,而且比必要的更复杂。
乔纳和科林·奥德尔的方法很有趣,但也过于沉重。
Orbling 非常接近他的
preg_replace
方法,它去掉了太多逗号,而且也没有考虑到 Needle 是唯一值的可能性。str_replace
和preg_replace
都具有明显的优势,即无需费心explode
和implode
。这需要成为此页面上答案的主要分隔符。将字符串转换为数组并返回字符串的方法(更不用说额外的处理)将比_replace
方法效率低。我还要注意到,没有人在他们的
explode
/implode
方法中提到array_diff
,因此我将其包含在我的方法列表中,尽管它依赖于explode
和implode
。这纯粹是为了演示。如果 Needle 是另一个值的子字符串,则所有
str_replace
方法都可能不可信。因为OP的示例输入在这方面没有提供冲突,所以我构建了一个str_replace
方法。我的一行
preg_replace
方法简洁而可靠。通过使用单词边界 (
\b
),我的正则表达式模式可以防止针存在于另一个值内的可能性。 查看 通过检查针是否带有前导逗号,然后是尾随逗号,然后没有逗号;如果字符串中唯一的值是针,则该模式仍然有效。最后,如果 csv 字符串中不存在 Needle,则不会出现错误。这是 SO 读者应该实现的答案。如果您要动态生成针,则可以通过两种方式声明您的模式:(
演示
)第二名产品使用
str_replace
:它替换
facebook
,然后用单个逗号替换所有双逗号,然后从结果字符串中删除所有前导或尾随逗号。第三位,
explode
->array_diff
->implode
作为单行:这是一个紧凑的单行,但仍然必须不厌其烦地爆炸、过滤和内爆。
这是我的测试场,我在这里对所有三种方法进行了测试。
RobertPitt's answer is the only one that bothers to loop and is more complicated than necessary.
Jonah's and Colin O'Dell's methods are interesting but also heavier than necessary.
Orbling got really close with his
preg_replace
method, it takes too many commas away and also doesn't account for the possibility of the needle being the only value.str_replace
andpreg_replace
both have the clear advantage of not bothering toexplode
andimplode
. This needs to be the primary separator of the answers on this page. Methods that convert the string to an array and back to a string (not to mention additional handling) will be less efficient than the_replace
methods.I'll also note that no one mentioned
array_diff
in theirexplode
/implode
methods so I'll include it in my list of methods even though it relies onexplode
andimplode
. It is purely for demonstration's sake.All
str_replace
methods are potentially untrustworthy if the needle is a substring of another value. Because the OP's sample input doesn't offer a conflict in this regard, I've built astr_replace
method.My one-liner
preg_replace
method is lean and solid.By using word boundaries (
\b
), my regex pattern is guarded against the possibility of a needle existing inside another value. See By checking for the needle with a leading comma, then a trailing comma, then no comma; the pattern still holds up if the only value in the string is the needle. Finally, if the needle is not present in the csv string, there is no error. THIS is the answer that SO readers should be implementing.If you are generating the needle dynamically, you can declare your pattern two ways:
(Demo)
My second place offering uses
str_replace
:It replaces
facebook
then replaces any double-commas with a single comma, then trims off any leading or trailing commas from the resulting string.And in third place,
explode
->array_diff
->implode
as a one-liner:It's a tight one-liner, but still has to go to the trouble to explode, filter, and implode.
Here is my testing ground where I put all three methods through the paces.