If you insist on regex, you can use positive look ahead as
preg_replace('/,(?=}$)/', '', "helloworld,}")
// helloworld}
Regex Explanation
-
,
Matches,
-
(?=}$)
positive look ahead. Checks if the,
is followed by}
and then end of line$
7
solved How can I check if the last 2 characters of a string are ,} and if yes remove the comma from it?