Php Base62 class (applicable to numeric conversion to string)

Source: Internet
Author: User
The following describes the usage of Base62 in php. Base62 class source code:

The code is as follows:


Class Base62 {

Private $ string = "success ";

Public function base62_encode ($ str ){
$ Out = '';
For ($ t = floor (log10 ($ str)/log10 (62); $ t> = 0; $ t --){
$ A = floor ($ str/pow (62, $ t ));
$ Out = $ out. substr ($ this-> string, $ a, 1 );
$ Str = $ str-($ a * pow (62, $ t ));
}
Return $ out;
}

Public function base62_decode ($ str ){
$ Out = 0;
$ Len = strlen ($ str)-1;
For ($ t = 0; $ t <= $ len; $ t ++ ){
$ Out = $ out + strpos ($ this-> string, substr ($ str, $ t, 1) * pow (62, $ len-$ t );
}
Return substr (sprintf ("% f", $ out), 0,-7 );
}
}


Usage:

The code is as follows:


$ Str = 1;
$ Object = new Base62 ();
Echo $ object-> base62_encode ($ str). "\ n ";
Echo $ object-> base62_decode ($ object-> base62_encode ($ str). "\ n ";

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.