public static clean ( string $str, array $options = [] ) : string | ||
$str | string | The string to clean. |
$options | array | 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. |
return | string | The cleaned string. |
public static function clean($str, array $options = array())
{
if (is_array($options['clean'])) {
$clean = $options['clean'];
} else {
$clean = array('method' => is_bool($options['clean']) ? 'text' : $options['clean']);
}
switch ($clean['method']) {
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;
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']) {
return static::clean($str, array('clean' => array('method' => 'text')) + $options);
}
break;
}
return $str;
}
/** * test Clean Insert * * @return void */ public function testCleanInsert() { $result = String::clean(':incomplete', array('clean' => true, 'before' => ':', 'after' => '')); $this->assertEqual('', $result); $result = String::clean(':incomplete', array('clean' => array('method' => 'text', 'replacement' => 'complete'), 'before' => ':', 'after' => '')); $this->assertEqual('complete', $result); $result = String::clean(':in.complete', array('clean' => true, 'before' => ':', 'after' => '')); $this->assertEqual('', $result); $result = String::clean(':in.complete and', array('clean' => true, 'before' => ':', 'after' => '')); $this->assertEqual('', $result); $result = String::clean(':in.complete or stuff', array('clean' => true, 'before' => ':', 'after' => '')); $this->assertEqual('stuff', $result); $result = String::clean('<p class=":missing" id=":missing">Text here</p>', array('clean' => 'html', 'before' => ':', 'after' => '')); $this->assertEqual('<p>Text here</p>', $result); $string = ':a 2 3'; $result = String::clean($string, array('clean' => true, 'before' => ':', 'after' => '')); $this->assertEqual('2 3', $result); $result = String::clean($string, array('clean' => false, 'before' => ':', 'after' => '')); $this->assertEqual($string, $result); }