how to remove comma white space during explode and replace?
$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.
$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
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?
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()
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.
精彩评论