| | 131 | |
| | 132 | /** |
| | 133 | * Word censor |
| | 134 | * |
| | 135 | * @access public |
| | 136 | * @param string |
| | 137 | * @param mixed |
| | 138 | * @param string |
| | 139 | * @param boolean |
| | 140 | * @return string |
| | 141 | */ |
| | 142 | public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = FALSE) |
| | 143 | { |
| | 144 | if ( ! is_array($badwords)) |
| | 145 | { |
| | 146 | $badwords = (array) $badwords; |
| | 147 | } |
| | 148 | |
| | 149 | foreach ($badwords as $key => $badword) |
| | 150 | { |
| | 151 | $badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword)); |
| | 152 | } |
| | 153 | |
| | 154 | $regex = '('.implode('|', $badwords).')'; |
| | 155 | |
| | 156 | if ( ! $replace_partial_words) |
| | 157 | { |
| | 158 | // Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself |
| | 159 | $regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)'; |
| | 160 | } |
| | 161 | |
| | 162 | $regex = '!'.$regex.'!ui'; |
| | 163 | |
| | 164 | if (utf8::strlen($replacement) == 1) |
| | 165 | { |
| | 166 | $regex .= 'e'; |
| | 167 | return preg_replace($regex, 'str_repeat($replacement, utf8::strlen(\'$1\'))', $str); |
| | 168 | } |
| | 169 | |
| | 170 | return preg_replace($regex, $replacement, $str); |
| | 171 | } |