Skip to content

Commit

Permalink
Merge pull request #20 from tuanht/master
Browse files Browse the repository at this point in the history
Added option to filter by word only
  • Loading branch information
snipe committed Aug 4, 2015
2 parents f56bc5c + a78fc03 commit f75d765
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/CensorWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public function randCensor($chars, $len) {
/**
* Apply censorship to $string, replacing $badwords with $censorChar.
* @param string $string String to be censored.
* @param bool $fullWords Option to censor by word only.
* string[string]
*/
public function censorString($string) {
public function censorString($string, $fullWords = false) {
$badwords = $this->badwords;
$anThis = &$this;

Expand Down Expand Up @@ -114,7 +115,11 @@ public function censorString($string) {
$words = explode(" ", $string);

for ($x=0; $x<count($badwords); $x++) {
$badwords[$x] = '/'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'/i';
if($fullWords) {
$badwords[$x] = '/\b'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'\b/i';
} else {
$badwords[$x] = '/'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'/i';
}
}

$counter=0;
Expand Down
15 changes: 14 additions & 1 deletion tests/CensorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public function testFuckeryClean()

}

public function testWordFuckeryClean()
{
$censor = new CensorWords;
$string = $censor->censorString('abc fuck xyz', true);
$this->assertEquals('abc **** xyz', $string['clean']);

$string2 = $censor->censorString('Hello World', true);
$this->assertEquals('Hello World', $string2['clean']);

$string3 = $censor->censorString('fuck...', true);
$this->assertEquals('****...', $string3['clean']);
}

public function testFuckeryOrig()
{
$censor = new CensorWords;
Expand Down Expand Up @@ -70,4 +83,4 @@ public function testSameCensorObj()
}


}
}

0 comments on commit f75d765

Please sign in to comment.