如何在爆炸和替换过程中删除逗号空格?

发布于 2024-10-17 09:01:04 字数 239 浏览 2 评论 0原文

$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 技术交流群。

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

发布评论

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

评论(6

红焚 2024-10-24 09:01:04

您的代码应如下所示:

$data = "google,facebook,youtube,twitter,bing";
$exp = explode(",",$data);

foreach($exp as $key => $item)
{
   if(trim($item) == "facebook")
   {
       unset($exp[$key]); //Remove from teh array.
   }
}

$final = implode(",",$rep);
echo $final;

或者只要您的角点后没有空格,您就可以简单地

$data = str_replace(",facebook,",",",$data);

使用str_replace 来解决许多复杂问题>,就用loopy方法吧。

Here's what your code should look like:

$data = "google,facebook,youtube,twitter,bing";
$exp = explode(",",$data);

foreach($exp as $key => $item)
{
   if(trim($item) == "facebook")
   {
       unset($exp[$key]); //Remove from teh array.
   }
}

$final = implode(",",$rep);
echo $final;

or as long as you have no spaces within after your comers you can simply go

$data = str_replace(",facebook,",",",$data);

To many complications using the str_replace, just use the loopy method.

你与清晨阳光 2024-10-24 09:01:04
$data = "google,facebook,youtube,twitter,bing";

$exp = explode(',', $data);
$index = array_search('facebook', $exp);
if ($index !== false){
    unset($exp[$index]);
}

$final = implode(',', $exp);

http://php.net/array-search

$data = "google,facebook,youtube,twitter,bing";

$exp = explode(',', $data);
$index = array_search('facebook', $exp);
if ($index !== false){
    unset($exp[$index]);
}

$final = implode(',', $exp);

http://php.net/array-search

私藏温柔 2024-10-24 09:01:04

您可以使用array_filter($data)从数组中删除空元素:

$data = str_replace("facebook", "", "google,facebook,youtube,twitter,bing");

$exp = array_filter(explode(",",$data));

$final = implode(",",$rep);

echo $final;

http://php.net/manual/en/function.array-filter.php

"如果未提供回调,则所有等于 FALSE 的输入条目(请参阅转换为布尔值)将被删除。 ”

这就是您要找的吗?

You can remove empty elements from an array using array_filter($data):

$data = str_replace("facebook", "", "google,facebook,youtube,twitter,bing");

$exp = array_filter(explode(",",$data));

$final = implode(",",$rep);

echo $final;

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?

何止钟意 2024-10-24 09:01:04

有很多方法可以做到这一点,但也许最简单的就是:

$data = str_replace(',,', ',', $data);

There are many ways to do this but perhaps the simplest is just:

$data = str_replace(',,', ',', $data);
说不完的你爱 2024-10-24 09:01:04
$data = "google,facebook,youtube,twitter,bing";

$final = preg_replace("/(^facebook,?|,facebook,|,facebook$)/", "", $data);

或者:

$final = implode(',', preg_grep("/^facebook$/", explode(',', $data), PREG_GREP_INVERT));

参见 preg_grep()

$data = "google,facebook,youtube,twitter,bing";

$final = preg_replace("/(^facebook,?|,facebook,|,facebook$)/", "", $data);

Alternatively:

$final = implode(',', preg_grep("/^facebook$/", explode(',', $data), PREG_GREP_INVERT));

See preg_grep()

沫雨熙 2024-10-24 09:01:04

罗伯特皮特的答案是唯一一个费心循环的答案,而且比必要的更复杂。
乔纳和科林·奥德尔的方法很有趣,但也过于沉重。

Orbling 非常接近他的 preg_replace 方法,它去掉了太多逗号,而且也没有考虑到 Needle 是唯一值的可能性。

str_replacepreg_replace 都具有明显的优势,即无需费心explodeimplode。这需要成为此页面上答案的主要分隔符。将字符串转换为数组并返回字符串的方法(更不用说额外的处理)将比 _replace 方法效率低。

我还要注意到,没有人在他们的 explode/implode 方法中提到 array_diff,因此我将其包含在我的方法列表中,尽管它依赖于 explodeimplode。这纯粹是为了演示。

如果 Needle 是另一个值的子字符串,则所有 str_replace 方法都可能不可信。因为OP的示例输入在这方面没有提供冲突,所以我构建了一个 str_replace 方法。

我的一行 preg_replace 方法简洁而可靠。

echo preg_replace("/,facebook\b|\bfacebook,|\bfacebook\b/","",$data);

通过使用单词边界 (\b),我的正则表达式模式可以防止针存在于另一个值内的可能性。 查看 通过检查针是否带有前导逗号,然后是尾随逗号,然后没有逗号;如果字符串中唯一的值是针,则该模式仍然有效。最后,如果 csv 字符串中不存在 Needle,则不会出现错误。这是 SO 读者应该实现的答案。

如果您要动态生成针,则可以通过两种方式声明您的模式:(

$needle="facebook";

echo preg_replace("/,$needle\b|\b$needle,|\b$needle\b/","",$data);
// OR
echo preg_replace("/,".$needle."\b|\b".$needle.",|\b".$needle."\b/","",$data);

演示


)第二名产品使用 str_replace

$args=[["facebook",",,"],["",","]];
echo trim(str_replace($args[0],$args[1],$data),",");

它替换 facebook,然后用单个逗号替换所有双逗号,然后从结果字符串中删除所有前导或尾随逗号。


第三位,explode->array_diff->implode 作为单行:

echo implode(',',array_diff(explode(',',$data),["facebook"]));

这是一个紧凑的单行,但仍然必须不厌其烦地爆炸、过滤和内爆。

这是我的测试场,我在这里对所有三种方法进行了测试。

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 and preg_replace both have the clear advantage of not bothering to explode and implode. 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 their explode/implode methods so I'll include it in my list of methods even though it relies on explode and implode. 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 a str_replace method.

My one-liner preg_replace method is lean and solid.

echo preg_replace("/,facebook\b|\bfacebook,|\bfacebook\b/","",$data);

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:

$needle="facebook";

echo preg_replace("/,$needle\b|\b$needle,|\b$needle\b/","",$data);
// OR
echo preg_replace("/,".$needle."\b|\b".$needle.",|\b".$needle."\b/","",$data);

(Demo)


My second place offering uses str_replace:

$args=[["facebook",",,"],["",","]];
echo trim(str_replace($args[0],$args[1],$data),",");

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:

echo implode(',',array_diff(explode(',',$data),["facebook"]));

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.

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