0

I want to remove the excess comma in my output string inside a parenthesis.

 $data= "item_id IN ( '1','2',) AND nt_id IN ( 'er1','er2',) AND";

I removed the Excess 'AND' by using rtrim funtion.

$trimValues = rtrim($data,'AND') ;

But how can I remove the comma inside a parenthesis?

Grr
  • 15,553
  • 7
  • 65
  • 85
Amboom
  • 156
  • 1
  • 13

1 Answers1

1
,(?=\s*\))

You can use this and replace by empty string.See demo.

https://regex101.com/r/rkDV4X/1

$re = '/,(?=\s*\))/';
$str = 'item_id IN ( \'1\',\'2\',) AND nt_id IN ( \'er1\',\'er2\',) AND';
$subst = '';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;
vks
  • 67,027
  • 10
  • 91
  • 124