Changeset 442

Show
Ignore:
Timestamp:
08/29/2007 10:30:18 AM (15 months ago)
Author:
Geert
Message:

text::censor() added

I started from CI's word_censor() helper, but had to improve it a lot:

  • You can now pass a string as 2nd argument, the function will wrap it into an array automatically.
  • If your replacement is a single character (any character) the badword will be fully masked with it, only possible with '#' in CI.
  • Bugfix: badwords that contain word boundaries themselves didn't get replaced
  • Added support for partial replacements (so f*cking becomes ####ing when searching for f*ck). Trigger it by the 4th parameter.
  • UTF-8 compatible
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/text.php

    r440 r442  
    9797        public static function random($type = 'alnum', $length = 8) 
    9898        { 
    99                 if ($type == 'unique') 
    100                         return md5(uniqid(mt_rand())); 
    101  
    10299                switch ($type) 
    103100                { 
     101                        case 'unique': 
     102                                return md5(uniqid(mt_rand())); 
    104103                        case '': 
    105104                        case 'alnum': 
     
    130129                return $str; 
    131130        } 
     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        } 
    132172 
    133173} // End text class