Search code examples
phpstringstr-replacetrim

Delete a character before another character


I need to delete comma , before each closing bracket ).

Example string:

$string = "('abc', 'def', 'gah',), ('qwe', 'rty', 'yui',)"

I have tried multiple times combining substr, str_replace. It just gives me wrong results.

Could you please help me out with this?


Solution

  • you need to replace ",)" by ")"

    $string = "('abc', 'def', 'gah',), ('qwe', 'rty', 'yui',)";
    $string = str_replace(",)", ")", $string);