private static function _decodeCrockford($to, $encodedStr, $hasCheckSymbol = false) {
        // Check input string.
        if ($hasCheckSymbol) {
            $pattern = '/[0-9A-TV-Z-]*[0-9A-Z*~$=]-*/i';
        } else {
            $pattern = '/[0-9A-TV-Z-]*/i';
        }
        self::_checkEncodedString($encodedStr, $pattern);

        // Remove hyphens from encoded string.
        $encodedStr = str_replace('-', '', $encodedStr);

        // Get length (byte count) of encoded string.
        $encodedStrLen = self::_byteCount($encodedStr);

        // If the last character is a valid Crockford check symbol, remove it from
        // the encoded string.
        if ($hasCheckSymbol) {
            $checkSymbol = $encodedStr[$encodedStrLen - 1];
            $encodedStr = self::_extractBytes($encodedStr, 0, $encodedStrLen - 1);
            $encodedStrLen--;
        }

        // Compose Crockford decoding mapping.
        $mapping = self::$_crockfordFlippedAlphabet + self::$_crockfordAdditionalCharMapping + array('_' => 0);

        // Get decoded content.
        if ($to == 'byteStr') {
            $decoded = self::_decodeToByteStr($encodedStr, $encodedStrLen, $mapping, '_', true);
        } elseif ($to == 'intStr') {
            $decoded = self::_crockfordDecodeToIntStr($encodedStr, $encodedStrLen, $mapping);
        }

        // If check symbol is present, check if decoded string is correct.
        if ($hasCheckSymbol) {
            if ($to == 'byteStr') {
                $intStr = self::_byteStrToIntStr($decoded);
            } elseif ($to == 'intStr') {
                $intStr = $decoded;
            }
            $modulus = (int) self::_intStrModulus($intStr, 37);
            if ($modulus != $mapping[$checkSymbol]) {
                throw new UnexpectedValueException('Check symbol does not match data.', self::E_CHECKSYMBOL_DOES_NOT_MATCH_DATA);
            }
        }

        // Return byte-string.
        return $decoded;
    }
private static function _decodeCrockford($to, $encodedStr, $hasCheckSymbol = false) {
        // Check input string.
        if ($hasCheckSymbol) {
            $pattern = '/[0-9A-TV-Z-]*[0-9A-Z*~$=]-*/i';
        } else {
            $pattern = '/[0-9A-TV-Z-]*/i';
        }
        self::_checkEncodedString($encodedStr, $pattern);

        // Remove hyphens from encoded string.
        $encodedStr = str_replace('-', '', $encodedStr);

        // Get length (byte count) of encoded string.
        $encodedStrLen = self::_byteCount($encodedStr);

        // If the last character is a valid Crockford check symbol, remove it from
        // the encoded string.
        if ($hasCheckSymbol) {
            $checkSymbol = $encodedStr[$encodedStrLen - 1];
            $encodedStr = self::_extractBytes($encodedStr, 0, $encodedStrLen - 1);
            $encodedStrLen--;
        }

        // Compose Crockford decoding mapping.
        $mapping = self::$_crockfordFlippedAlphabet + self::$_crockfordAdditionalCharMapping + array('_' => 0);

        // Get decoded content.
        if ($to == 'byteStr') {
            $decoded = self::_decodeToByteStr($encodedStr, $encodedStrLen, $mapping, '_', true);
        } elseif ($to == 'intStr') {
            $decoded = self::_crockfordDecodeToIntStr($encodedStr, $encodedStrLen, $mapping);
        }

        // If check symbol is present, check if decoded string is correct.
        if ($hasCheckSymbol) {
            if ($to == 'byteStr') {
                $intStr = self::_byteStrToIntStr($decoded);
            } elseif ($to == 'intStr') {
                $intStr = $decoded;
            }
            $modulus = (int) self::_intStrModulus($intStr, 37);
            if ($modulus != $mapping[$checkSymbol]) {
                throw new UnexpectedValueException('Check symbol does not match data.', self::E_CHECKSYMBOL_DOES_NOT_MATCH_DATA);
            }
        }

        // Return byte-string.
        return $decoded;
    }
private static function _decodeCrockford ( $ to , $ encodedStr , $ hasCheckSymbol = false ) { // Check input string. if ( $ hasCheckSymbol ) { $ pattern = '/[0-9A-TV-Z-]*[0-9A-Z*~$=]-*/i' ; } else { $ pattern = '/[0-9A-TV-Z-]*/i' ; } self :: _checkEncodedString ( $ encodedStr , $ pattern ) ; // Remove hyphens from encoded string. $ encodedStr = str_replace ( '-' , '' , $ encodedStr ) ; // Get length (byte count) of encoded string. $ encodedStrLen = self :: _byteCount ( $ encodedStr ) ; // If the last character is a valid Crockford check symbol, remove it from // the encoded string. if ( $ hasCheckSymbol ) { $ checkSymbol = $ encodedStr [ $ encodedStrLen - 1 ] ; $ encodedStr = self :: _extractBytes ( $ encodedStr , 0 , $ encodedStrLen - 1 ) ; $ encodedStrLen -- ; } // Compose Crockford decoding mapping. $ mapping = self :: $ _crockfordFlippedAlphabet + self :: $ _crockfordAdditionalCharMapping + array ( '_' => 0 ) ; // Get decoded content. if ( $ to == 'byteStr' ) { $ decoded = self :: _decodeToByteStr ( $ encodedStr , $ encodedStrLen , $ mapping , '_' , true ) ; } elseif ( $ to == 'intStr' ) { $ decoded = self :: _crockfordDecodeToIntStr ( $ encodedStr , $ encodedStrLen , $ mapping ) ; } // If check symbol is present, check if decoded string is correct. if ( $ hasCheckSymbol ) { if ( $ to == 'byteStr' ) { $ intStr = self :: _byteStrToIntStr ( $ decoded ) ; } elseif ( $ to == 'intStr' ) { $ intStr = $ decoded ; } $ modulus = ( int ) self :: _intStrModulus ( $ intStr , 37 ) ; if ( $ modulus != $ mapping [ $ checkSymbol ] ) { throw new UnexpectedValueException ( 'Check symbol does not match data.' , self :: E_CHECKSYMBOL_DOES_NOT_MATCH_DATA ) ; } } // Return byte-string. return $ decoded ; }
Decodes a Crockford base32 encoded string.

Expects an encoded string according to the base 32 encoding exposed by
Douglas Crockford (http://www.crockford.com/wrmg/base32.html). Throws an
exception if the encoded string is malformed.

If a check symbol is provided and does not match the decoded data, an
exception is thrown.

@param string $to String to specify whether to decode to an integer
('intStr') or to a byte-string ('byteStr').
@param string $encodedStr The encoded string.
@param bool $hasCheckSymbol If true, the last character of the encoded
string is regarded as check symbol.
@return mixed A byte-string (for $to = 'byteStr') or a non-negative
integer or a string representing a non-negative integer (for $to
= 'intStr').
@throws UnexpectedValueException
Decodes a Crockford base32 encoded string .