Esempio n. 1
0
 /**
  * Replaces the given words with a string.
  *
  *     // Displays "What the #####, man!"
  *     echo Text::censor('What the frick, man!', array(
  *         'frick' => '#####',
  *     ));
  *
  * @param   string  $str                    phrase to replace words in
  * @param   array   $badwords               words to replace
  * @param   string  $replacement            replacement string
  * @param   boolean $replace_partial_words  replace words across word boundaries (space, period, etc)
  * @return  string
  * @uses    UTF8::strlen
  */
 public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
 {
     foreach ((array) $badwords as $key => $badword) {
         $badwords[$key] = str_replace('\\*', '\\S*?', preg_quote((string) $badword));
     }
     $regex = '(' . implode('|', $badwords) . ')';
     if ($replace_partial_words === FALSE) {
         // Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
         $regex = '(?<=\\b|\\s|^)' . $regex . '(?=\\b|\\s|$)';
     }
     $regex = '!' . $regex . '!ui';
     if (UTF8::strlen($replacement) == 1) {
         $regex .= 'e';
         return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $str);
     }
     return preg_replace($regex, $replacement, $str);
 }
Esempio n. 2
0
 /**
  * Check an email address for correct format.
  *
  * @link  http://www.iamcal.com/publish/articles/php/parsing_email/
  * @link  http://www.w3.org/Protocols/rfc822/
  *
  * @param   string  $email  email address
  * @param   boolean $strict strict RFC compatibility
  * @return  boolean
  */
 public static function email($email, $strict = FALSE)
 {
     if (UTF8::strlen($email) > 254) {
         return FALSE;
     }
     if ($strict === TRUE) {
         $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
         $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
         $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
         $pair = '\\x5c[\\x00-\\x7f]';
         $domain_literal = "\\x5b({$dtext}|{$pair})*\\x5d";
         $quoted_string = "\\x22({$qtext}|{$pair})*\\x22";
         $sub_domain = "({$atom}|{$domain_literal})";
         $word = "({$atom}|{$quoted_string})";
         $domain = "{$sub_domain}(\\x2e{$sub_domain})*";
         $local_part = "{$word}(\\x2e{$word})*";
         $expression = "/^{$local_part}\\x40{$domain}\$/D";
     } else {
         $expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\\.[a-z]{2,6}|\\d{1,3}(?:\\.\\d{1,3}){3})$/iD';
     }
     return (bool) preg_match($expression, (string) $email);
 }