diff --git a/src/Check.php b/src/Check.php index f7a38c6..358243d 100644 --- a/src/Check.php +++ b/src/Check.php @@ -141,7 +141,12 @@ class Check */ protected $profanities = []; protected $separatorExpression; - protected $characterExpressions; + protected $characterExpressions; + + /** + * Saved bad words found. + */ + protected $badWordsFound = ''; /** * @param null $config @@ -239,6 +244,27 @@ public function obfuscateIfProfane($string) $string = str_repeat("*", strlen($string)); } + return $string; + } + + /** + * Obfuscates only the string that contains a 'profanity'. + * + * @param $string + * + * @return string + */ + public function cleanWords($string, $replacement = '*') + { + if ($this->hasProfanity($string)) { + $profanity = $this->generateProfanityExpression( + $this->badWordsFound, + $this->characterExpressions, + $this->separatorExpression + ); + $string = preg_replace($profanity, str_repeat($replacement, strlen($this->badWordsFound)), $string); + } + return $string; } @@ -251,6 +277,8 @@ public function obfuscateIfProfane($string) */ public function hasProfanity($string) { + $this->badWordsFound = ''; + if (empty($string)) { return false; } @@ -266,8 +294,9 @@ public function hasProfanity($string) ); } - foreach ($profanities as $profanity) { + foreach ($profanities as $i => $profanity) { if ($this->stringHasProfanity($string, $profanity)) { + $this->badWordsFound = $this->profanities[$i]; return true; } } @@ -275,6 +304,14 @@ public function hasProfanity($string) return false; } + /** + * Get bad words found + */ + public function getBadWordsFound() + { + return $this->badWordsFound; + } + /** * Generate a regular expression for a particular word *