add(). If they * are a string, it is passed to $this->mask(). */ if (is_array($rules)) { return $this->add($rules); } if (is_string($rules)) { return $this->mask($rules); } if (!$rules) { return True; } return False; } public function mask($chars, $mask) { /* Adds automatically rules for masking all given chars. */ $chars = $mask.$chars; for ($i = 0; $i < strlen($chars); $i++) { $this->add($chars[$i], $mask.ord($chars[$i])); } return True; } public function add($search, $replace = "") { /* Adds a new rule which text occurrences should be replaced by * $this->modify(). If $search is an array, array keys are replaced by * array values. */ if (is_string($search)) { $this->rules[$search] = $replace; return True; } if (is_array($search)) { $this->rules = array_merge($this->rules, $search); return True; } return False; } public function clear() { /* Clears all rules created by $this->add(). */ $this->rules = array(); return True; } public function invert($invert = True) { /* Decides wether $search and $replace variables should be switched. */ $this->invert = $invert; return True; } public function modify($needle) { /* Executes configured replacements. */ if (is_array($needle)) { $ret = $needle; } else if (is_string($needle)) { $ret = array($needle); } else { return False; } foreach($this->rules as $search => $replace) { if ($this->invert) { $tmp = $search ; $search = $replace; $replace = $tmp ; } foreach($ret as $key => $string) { if ($this->onlyfirst) { $ret[$key] = self::replacefirstoccurrence($search, $replace, $string); } else { $ret[$key] = str_replace($search, $replace, $string); } } } if (is_string($needle)) { return $ret[0]; } return $ret; } public function replaceonlyfirstoccurrences($onlyfirst = True) { /* Configure wether only first string occurrences should be replaced by * $this->modify() */ $this->onlyfirst = $onlyfirst; return True; } public static function between($string, $start, $end) { /* Finds the string in $text between $start and $end. */ $start = strpos($string, $start) + strlen($start); $end = strpos($string, $end, $start) - $start ; return substr($string, $start, $end) ; } public static function firstoccurrence($search, $replace, $needle) { /* Replaces the first occurrence in $needle of $search with $replace. */ $pos = strpos($needle, $search); if ($pos === False) { return $needle; } return substr_replace($needle, $replace, $pos, strlen($search)); } } ?>