public static function clean($str, $options = [])
{
$options += ['before' => '{:', 'after' => '}', 'escape' => '\\', 'word' => '[\\w,.]+', 'gap' => '(\\s*(?:(?:and|or|,)\\s*)?)', 'replacement' => ''];
extract($options);
$begin = $escape ? '(?<!' . preg_quote($escape) . ')' . preg_quote($before) : preg_quote($before);
$end = preg_quote($options['after']);
$callback = function ($matches) use($replacement) {
if (isset($matches[2]) && isset($matches[3]) && trim($matches[2]) === trim($matches[3])) {
if (trim($matches[2]) || $matches[2] && $matches[3]) {
return $matches[2] . $replacement;
}
}
return $replacement;
};
$str = preg_replace_callback('/(' . $gap . $before . $word . $after . $gap . ')+/', $callback, $str);
if ($escape) {
$str = preg_replace('/' . preg_quote($escape) . preg_quote($before) . '/', $before, $str);
}
return $str;
}