- preg_replace_callback: documentation ( source)
- trim: documentation ( source)
- preg_replace: documentation ( source)
<?php
$strings = [
"I-IVWM_Ask-21_(max_2h)",
"D-7849_DG303_(max_4h)",
"Aliante_Privato"
];
foreach ($strings as $str) {
$pattern = "/^[^\s()]+(?:\([^()]*\))?$/";
echo preg_replace_callback($pattern, function ($x) {
return trim(preg_replace("/[()_]+/", " ", $x[0]));
}, $str) . PHP_EOL;
}