Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class Check
*/
protected $profanities = [];
protected $separatorExpression;
protected $characterExpressions;
protected $characterExpressions;

/**
* Saved bad words found.
*/
protected $badWordsFound = '';

/**
* @param null $config
Expand Down Expand Up @@ -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;
}

Expand All @@ -251,6 +277,8 @@ public function obfuscateIfProfane($string)
*/
public function hasProfanity($string)
{
$this->badWordsFound = '';

if (empty($string)) {
return false;
}
Expand All @@ -266,15 +294,24 @@ 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;
}
}

return false;
}

/**
* Get bad words found
*/
public function getBadWordsFound()
{
return $this->badWordsFound;
}

/**
* Generate a regular expression for a particular word
*
Expand Down