URL-Safe String base64 encoding and decoding

Source: Internet
Author: User

If you use the Base64_encode and Base64_decode methods directly, the resulting string may not be applicable to the URL address. The following methods can be used to resolve the problem:
URL-safe string encoding:

function Urlsafe_b64encode ($string) {$data = Base64_encode ($string);   $data = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), $data); return $data;}

URL-safe String decoding:

function Urlsafe_b64decode ($string) {$data = Str_replace (Array ('-', ' _ '), Array (' + ', '/'), $string);   $mod 4 = strlen ($data)% 4;   if ($mod 4) {$data. = substr (' = = = ', $mod 4); } return Base64_decode ($data);}


URL-Safe String base64 encoding and decoding

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.