From 845587b60d3cf8c743070854cd7281c3e64cf698 Mon Sep 17 00:00:00 2001 From: TheInfernoX Date: Fri, 12 Jun 2015 19:53:09 +0800 Subject: [PATCH 1/2] Added "matched" feature, simplified randCensor Added the "matched" element in $newstring which returns an array of the matched words. Also simplified the randCensor function. --- src/CensorWords.php | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/CensorWords.php b/src/CensorWords.php index 30f5335..26fd655 100644 --- a/src/CensorWords.php +++ b/src/CensorWords.php @@ -68,20 +68,8 @@ public function setReplaceChar($replacer) { */ public function randCensor($chars, $len) { - mt_srand(); // useful for < PHP4.2 - $lastChar = strlen($chars) - 1; - $randOld = -1; - $out = ''; - - // create $len chars - for ($i = $len; $i > 0; $i--) { - // generate random char - it must be different from previously generated - while (($randNew = mt_rand(0, $lastChar)) === $randOld) { } - $randOld = $randNew; - $out .= $chars[$randNew]; - } - - return $out; + return str_shuffle(str_repeat($chars, intval($len/strlen($chars))). + substr($chars, 0, ($len%strlen($chars)))); } @@ -93,6 +81,7 @@ public function randCensor($chars, $len) { */ public function censorString($string) { $badwords = $this->badwords; + $anThis = &$this; $leet_replace = array(); $leet_replace['a']= '(a|a\.|a\-|4|@|Á|á|À|Â|à|Â|â|Ä|ä|Ã|ã|Å|å|α|Δ|Λ|λ)'; @@ -124,23 +113,25 @@ public function censorString($string) { $words = explode(" ", $string); - // is $censorChar a single char? - $isOneChar = (strlen($this->replacer) === 1); - for ($x=0; $xreplacer,strlen($badwords[$x])) - : $this->randCensor($this->replacer,strlen($badwords[$x])); - $badwords[$x] = '/'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'/i'; } + $counter=0; + $match = array(); $newstring = array(); $newstring['orig'] = html_entity_decode($string); - $newstring['clean'] = preg_replace($badwords, $replacement, $newstring['orig']); + // $anThis for <= PHP5.3 + $newstring['clean'] = preg_replace_callback($badwords, function($matches) use (&$anThis,&$counter,&$match) { + $match[$counter++] = $matches[0]; + // is $anThis->replacer a single char? + return (strlen($anThis->replacer) === 1) + ? str_repeat($anThis->replacer, strlen($matches[0])) + : $anThis->randCensor($anThis->replacer, strlen($matches[0])); + }, $newstring['orig']); + $newstring['matched'] = $match; return $newstring; } -} \ No newline at end of file +} From 5559522847c38ee9c023e842481ec3e1aae2c16c Mon Sep 17 00:00:00 2001 From: TheInfernoX Date: Fri, 12 Jun 2015 19:57:22 +0800 Subject: [PATCH 2/2] Update CensorWords.php --- src/CensorWords.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CensorWords.php b/src/CensorWords.php index 26fd655..7c372b4 100644 --- a/src/CensorWords.php +++ b/src/CensorWords.php @@ -124,6 +124,7 @@ public function censorString($string) { // $anThis for <= PHP5.3 $newstring['clean'] = preg_replace_callback($badwords, function($matches) use (&$anThis,&$counter,&$match) { $match[$counter++] = $matches[0]; + // is $anThis->replacer a single char? return (strlen($anThis->replacer) === 1) ? str_repeat($anThis->replacer, strlen($matches[0]))