Answer by Subrata Mallik for How to remove unwanted commas from a string?
<?php$str = ",a,b,c,,,d,,"echo $str=str_replace(',,','',$str);?>Output:,a,b,c,d<?php $str = ",a,b,c,,,d,,"echo $str=trim(str_replace(',,','',$str),',');?>Output:a,b,c,d
View ArticleAnswer by neelsg for How to remove unwanted commas from a string?
Probably not very fast, but a simple method may be:$str = "a,b,c,,,d";$str2 = "";while($str <> $str2) { $str2 = $str; $str = str_replace(',,', ',', $str);}
View ArticleAnswer by Fabio for How to remove unwanted commas from a string?
i think you can just explode your string and then create a new one getting only relevant data$string = ",a,b,c,,,d,,";$str = explode(",", $string);$string_new = '';foreach($str as $data){...
View ArticleAnswer by Matteo Tassinari for How to remove unwanted commas from a string?
Try this:$str = preg_replace('/,{2,}/', ',', trim($str, ','));The trim will remove starting and trailing commas, while the preg_replace will remove duplicate ones.See it in action!Also, as @Spudley...
View ArticleHow to remove unwanted commas from a string?
How can i remove duplicate commas used in string.String = ",a,b,c,,,d,,"I tried rtrim and itrim functions and removed the unwanted commas from beginning and ending .How can i remove duplicate commas ?
View Article