@@ -13,6 +13,16 @@ class CensorWords
1313 */
1414 private $ censorChecks = null ;
1515
16+ /**
17+ * @var array
18+ */
19+ private $ whiteList = [];
20+
21+ /**
22+ * @var string
23+ */
24+ private $ whiteListPlaceHolder = ' {whiteList[i]} ' ;
25+
1626 public function __construct () {
1727 $ this ->badwords = array ();
1828 $ this ->replacer = '* ' ;
@@ -75,6 +85,43 @@ private function readBadWords($dictionary) {
7585 return $ badwords ;
7686 }
7787
88+ /**
89+ * List of word to add which will be overridden
90+ *
91+ * @param array $list
92+ */
93+ public function addWhileList (array $ list )
94+ {
95+ foreach ($ list as $ value ) {
96+ if (is_string ($ value ) && !empty ($ value )) {
97+ $ this ->whiteList []['word ' ] = $ value ;
98+ }
99+ }
100+ }
101+
102+ /**
103+ * Replace white listed words with placeholders and inversely
104+ *
105+ * @param $string
106+ * @param bool $reverse
107+ * @return mixed
108+ */
109+ private function replaceWhiteListed ($ string , $ reverse = false )
110+ {
111+ foreach ($ this ->whiteList as $ key => $ list ) {
112+ if ($ reverse && !empty ($ this ->whiteList [$ key ]['placeHolder ' ])) {
113+ $ placeHolder = $ this ->whiteList [$ key ]['placeHolder ' ];
114+ $ string = str_replace ($ placeHolder , $ list ['word ' ], $ string );
115+ } else {
116+ $ placeHolder = str_replace ('[i] ' , $ key , $ this ->whiteListPlaceHolder );
117+ $ this ->whiteList [$ key ]['placeHolder ' ] = $ placeHolder ;
118+ $ string = str_replace ($ list ['word ' ], $ placeHolder , $ string );
119+ }
120+ }
121+
122+ return $ string ;
123+ }
124+
78125 /**
79126 * Sets the replacement character to use
80127 *
@@ -171,6 +218,7 @@ public function censorString($string, $fullWords = false) {
171218 $ match = array ();
172219 $ newstring = array ();
173220 $ newstring ['orig ' ] = html_entity_decode ($ string );
221+ $ original = $ this ->replaceWhiteListed ($ newstring ['orig ' ]);
174222 // $anThis for <= PHP5.3
175223 $ newstring ['clean ' ] = preg_replace_callback (
176224 $ this ->censorChecks ,
@@ -182,8 +230,9 @@ function($matches) use (&$anThis,&$counter,&$match) {
182230 ? str_repeat ($ anThis ->replacer , strlen ($ matches [0 ]))
183231 : $ anThis ->randCensor ($ anThis ->replacer , strlen ($ matches [0 ]));
184232 },
185- $ newstring [ ' orig ' ]
233+ $ original
186234 );
235+ $ newstring ['clean ' ] = $ this ->replaceWhiteListed ($ newstring ['clean ' ], true );
187236 $ newstring ['matched ' ] = $ match ;
188237
189238 return $ newstring ;
0 commit comments