- preg_replace_callback: documentation ( source)
- preg_replace: documentation ( source)
<?php
$str = '(a,b),a,b,c,(a,f)';
$str = preg_replace_callback('`("|\').*?\1`', function ($matches) {
return preg_replace('`(?<!\\\)[()]`', '\\\;', $matches[0]);
}, $str);
echo $str;