PHP BASE62 encoding and decoding implementation

Source: Internet
Author: User

1<?PHP2 /**3 * @desc BASE62 encoding and decoding implementation4 * @param String $data5 * @author Space6 * @date 2014/8/67 **/8 9 functionBase62_encode ($data){Ten     $base 62str= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; One     $data=Strval($data); A     $base=Str_split($base 62str); -     $len=strlen($data); -     $i= 0; the     $TMPARR=Array(); -      while($i<$len){ -         $val=$data[$i]; -         $tmp=Str_pad(Decbin(Ord($data[$i]), 8, ' 0 ',str_pad_left); +         $temp=Str_split($tmp, 4); -         $TMPARR=Array_merge($TMPARR,$temp); +++$i; A     } at     $result= ' '; -     $i= 0; -     foreach($TMPARR  as $arr){ -         $temp=Bindec($arr) *4+$i%; -         $result.=$base[$temp]; -++$i; in     } -     return $result; to } +  - functionBase62_decode ($data){ the     $base 62str= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; *     $data=Strval($data); $     $base=Str_split($base 62str);Panax Notoginseng     $base 62ARR=Array_flip($base); -     if(!Preg_match('/[a-za-z0-9]+/',$data)){ the         return false; +     } A     $len=strlen($data); the     $i= 0; +     $TEMPARR=Array(); -      while($i<$len){ $         $temp=Decbin(($base 62ARR[$data[$i]]-$i%2)/4); $         $TEMPARR[] =Str_pad($temp, 4, ' 0 ',str_pad_left); -++$i; -     } the     $result= ' '; -     $TEMPARR=Array_chunk($TEMPARR, 2);Wuyi     foreach($TEMPARR  as $arr){ the         $result.=CHR(Bindec(Join(‘‘,$arr))); -     } Wu     return $result; -}

It is also possible to $base62str a variable in random order according to the need, which is equivalent to a simple encryption, but it has little meaning.

PHP BASE62 encoding and decoding implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.