HTMLPurifier_Encoder::unichr PHP Method

unichr() public static method

+----------+----------+----------+----------+
public static unichr ( $code )
    public static function unichr($code)
    {
        if ($code > 1114111 or $code < 0 or $code >= 55296 and $code <= 57343) {
            // bits are set outside the "valid" range as defined
            // by UNICODE 4.1.0
            return '';
        }
        $x = $y = $z = $w = 0;
        if ($code < 128) {
            // regular ASCII character
            $x = $code;
        } else {
            // set up bits for UTF-8
            $x = $code & 63 | 128;
            if ($code < 2048) {
                $y = ($code & 2047) >> 6 | 192;
            } else {
                $y = ($code & 4032) >> 6 | 128;
                if ($code < 65536) {
                    $z = $code >> 12 & 15 | 224;
                } else {
                    $z = $code >> 12 & 63 | 128;
                    $w = $code >> 18 & 7 | 240;
                }
            }
        }
        // set up the actual character
        $ret = '';
        if ($w) {
            $ret .= chr($w);
        }
        if ($z) {
            $ret .= chr($z);
        }
        if ($y) {
            $ret .= chr($y);
        }
        $ret .= chr($x);
        return $ret;
    }

Usage Example

示例#1
0
 /**
  * Callback function for substituteNonSpecialEntities() that does the work.
  * 
  * @warning Though this is public in order to let the callback happen,
  *          calling it directly is not recommended.
  * @param $matches  PCRE matches array, with 0 the entire match, and
  *                  either index 1, 2 or 3 set with a hex value, dec value,
  *                  or string (respectively).
  * @returns Replacement string.
  */
 function nonSpecialEntityCallback($matches)
 {
     // replaces all but big five
     $entity = $matches[0];
     $is_num = @$matches[0][1] === '#';
     if ($is_num) {
         $is_hex = @$entity[2] === 'x';
         $code = $is_hex ? hexdec($matches[1]) : (int) $matches[2];
         // abort for special characters
         if (isset($this->_special_dec2str[$code])) {
             return $entity;
         }
         return HTMLPurifier_Encoder::unichr($code);
     } else {
         if (isset($this->_special_ent2dec[$matches[3]])) {
             return $entity;
         }
         if (!$this->_entity_lookup) {
             $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
         }
         if (isset($this->_entity_lookup->table[$matches[3]])) {
             return $this->_entity_lookup->table[$matches[3]];
         } else {
             return $entity;
         }
     }
 }
All Usage Examples Of HTMLPurifier_Encoder::unichr