- preg_replace_callback: documentation ( source)
- strtoupper: documentation ( source)
<?php
$string = 'pippo ASC, `table`.`pippo` DESC, campo.discendente DESC, `tabella`.`asc` ASC';
$string = preg_replace_callback('#(![\`]+)\b(ASC|DESC)\b#i', function($match) {
switch(strtoupper($match[1])) {
case 'ASC': return 'DESC';
case 'DESC': return 'ASC';
}
}, $string);
echo $string;