コード例 #1
0
 /**
  * Returns the supplied input string after removing any characters not
  * present in the supplied whitelist.
  *
  * @param string $input     string input to be filtered.
  * @param string $whitelist array or string of whitelist characters.
  *
  * @return string returns characters from $input that are present in $whitelist.
  */
 public function whitelist($input, $whitelist)
 {
     // Sanity check
     if (!is_string($input) || $input == '') {
         $input = '';
     }
     if (is_string($whitelist)) {
         $charEnc = Codec::detectEncoding($whitelist);
         $limit = mb_strlen($whitelist, $charEnc);
         $ary = array();
         for ($i = 0; $i < $limit; $i++) {
             $ary[] = mb_substr($whitelist, $i, 1, $charEnc);
         }
         $whitelist = $ary;
     }
     $filtered = '';
     $initialCharEnc = Codec::detectEncoding($input);
     $_4ByteCharacterString = Codec::normalizeEncoding($input);
     $limit = mb_strlen($_4ByteCharacterString, 'UTF-32');
     for ($i = 0; $i < $limit; $i++) {
         $c = mb_substr($_4ByteCharacterString, $i, 1, 'UTF-32');
         if (Codec::containsCharacter($c, $whitelist)) {
             $filtered .= $c;
         }
     }
     if ($filtered != '') {
         $filtered = mb_convert_encoding($filtered, $initialCharEnc, 'UTF-32');
     }
     if (!is_string($filtered)) {
         $filtered = '';
     }
     return $filtered;
 }