Beispiel #1
0
 /**
  * Convert a phrase to a URL-safe title.
  *
  *     echo URL::title('My Blog Post'); // "my-blog-post"
  *
  * @param   string   $title       Phrase to convert
  * @param   string   $separator   Word separator (any single character)
  * @param   boolean  $ascii_only  Transliterate to ASCII?
  * @return  string
  * @uses    UTF8::transliterate_to_ascii
  */
 public function title($title, $separator = '-', $ascii_only = FALSE)
 {
     if ($ascii_only === TRUE) {
         // Transliterate non-ASCII characters
         $title = UTF8::transliterate_to_ascii($title);
         // Remove all characters that are not the separator, a-z, 0-9, or whitespace
         $title = preg_replace('![^' . preg_quote($separator) . 'a-z0-9\\s]+!', '', strtolower($title));
     } else {
         // Remove all characters that are not the separator, letters, numbers, or whitespace
         $title = preg_replace('![^' . preg_quote($separator) . '\\pL\\pN\\s]+!u', '', UTF8::strtolower($title));
     }
     // Replace all separator characters and whitespace by a single separator
     $title = preg_replace('![' . preg_quote($separator) . '\\s]+!u', $separator, $title);
     // Trim separators from the beginning and end
     return trim($title, $separator);
 }
Beispiel #2
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);
 }
Beispiel #3
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);
 }