Search code examples
phpstringreplace

Remove a specified character and one neighboring comma in string


I just want to remove character "c" from variable $a

$a = "a,b,c,d";

and the variable $a must in this format

$a = "a,b,d";

I tried array(), but it not useful for my work.


Solution

  • If you are going for one of the str_replace() methods don't forget to include an extra , in front of your string (and stripping it afterwards) because you would have trouble stripping of the first character (if chosen).

    $str = substr(str_replace(",c", "", "," . $a), 1);