Cleans up a `Set::insert()` formatted string with given `$options` depending
on the `'clean'` option. The goal of this function is to replace all whitespace
and unneeded mark-up around place-holders that did not get replaced by `Set::insert()`.
Parameters
- string $str The string to clean.
- array $options Available options are: - `'after'`: characters marking the end of targeted substring. - `'andText'`: (defaults to `true`). - `'before'`: characters marking the start of targeted substring. - `'clean'`: `true` or an array of clean options: - `'gap'`: Regular expression matching gaps. - `'method'`: Either `'text'` or `'html'` (defaults to `'text'`). - `'replacement'`: String to use for cleaned substrings (defaults to `''`). - `'word'`: Regular expression matching words.
Returns
string The cleaned string.Source
public static function clean($str, array $options = array()) {
if (!$options['clean']) {
return $str;
}
$clean = $options['clean'];
$clean = ($clean === true) ? array('method' => 'text') : $clean;
$clean = (!is_array($clean)) ? array('method' => $options['clean']) : $clean;
switch ($clean['method']) {
case 'html':
$clean += array('word' => '[\w,.]+', 'andText' => true, 'replacement' => '');
$kleenex = sprintf(
'/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
preg_quote($options['before'], '/'),
$clean['word'],
preg_quote($options['after'], '/')
);
$str = preg_replace($kleenex, $clean['replacement'], $str);
if ($clean['andText']) {
$options['clean'] = array('method' => 'text');
$str = static::clean($str, $options);
}
break;
case 'text':
$clean += array(
'word' => '[\w,.]+', 'gap' => '[\s]*(?:(?:and|or|,)[\s]*)?', 'replacement' => ''
);
$before = preg_quote($options['before'], '/');
$after = preg_quote($options['after'], '/');
$kleenex = sprintf(
'/(%s%s%s%s|%s%s%s%s|%s%s%s%s%s)/',
$before, $clean['word'], $after, $clean['gap'],
$clean['gap'], $before, $clean['word'], $after,
$clean['gap'], $before, $clean['word'], $after, $clean['gap']
);
$str = preg_replace($kleenex, $clean['replacement'], $str);
break;
}
return $str;
}