- preg_replace_callback: documentation ( source)
- str_replace: documentation ( source)
<?php
$ingredients = "aaa (1,2,3) bbb [4,5,6,7]";
$clean_ingredients = preg_replace_callback('/\([^()]*\)|\[[^][]*]/', function($m) {return str_replace(',', '! ', $m[0]);}, $ingredients );
echo $clean_ingredients;