$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?
$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?
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.
array_search()
is more efficient than a loop. See my answer. –
Byelaw array_search
more * efficient* then a foreach
? –
Distribute $data = "google,facebook,youtube,twitter,bing";
$exp = explode(',', $data);
$index = array_search('facebook', $exp);
if ($index !== false){
unset($exp[$index]);
}
$final = implode(',', $exp);
false
would be interpreted as 0
thus removing the first element of $exp
. > codepad.org/5GTBp1r4 –
Distribute 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?
"barfacebookfoo"
. –
Byelaw "barfacebookfoo"
, they can easily change line 1 to use a preg_replace()
with a pattern like \w*facebook\w*
. –
Esemplastic There are many ways to do this but perhaps the simplest is just:
$data = str_replace(',,', ',', $data);
$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()
preg_replace()
over str_replace()
even str_replace()
can handle arrays. –
Distribute str_replace(array("facebook,","facebook"),"",$data)
? –
Distribute $final = "google,youtube,twitter,bing";
and be done with it. –
Socratic str_replace()
can be guaranteed to apply the searches in the order present in the array, then yes. If it took 'facebook'
on its own first, then it would ruin it. –
Socratic constant
I meant consistent
, i.e. No spaces following the keywords –
Distribute "foofacebookbar"
and you used str_replace, the result would be "foobar"
–
Byelaw foobarfacebook,
existed. –
Socratic "google,foofacebook,youtube,twitter,bing"
breaks it. –
Byelaw str_replace(array("facebook,",",facebook,",",facebook"),",",$data)
–
Distribute (,facebook,)|(\Afacebook,)|(,facebook\Z)|(\Afacebook\Z)
–
Byelaw "google,facebookbar,youtube,twitter"
breaks the regex option. –
Byelaw 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.
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.
© 2022 - 2024 — McMap. All rights reserved.
unset()
the array element itself (e.g. remove it from the array completely), not just set it to a blank value. – Arbitrage