Home > Backend Development > PHP Tutorial > How does php base64 encode and decode URL strings?

How does php base64 encode and decode URL strings?

青灯夜游
Release: 2023-04-09 06:08:01
forward
3393 people have browsed it

How does php base64 encode and decode URL strings?

Base64 can transcode binary into visible characters for http transmission, but when base64 transcodes, it will generate " ", "/", "=", which are transcoded by the URL. Special characters cause inconsistency in the two aspects of data.

We can replace " ", "/", and "=" with characters that will not be transcoded in the URL before sending. After receiving the data, we can replace these characters back and then decode.

1. URL safe string encoding:

    function urlsafe_b64encode($string) {
       $data = base64_encode($string);
       $data = str_replace(array('+','/','='),array('-','_',''),$data);
       return $data;
    }
Copy after login

2. URL safe string decoding:

    function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return base64_decode($data);
    }
Copy after login

Recommended tutorial: "php tutorial"

The above is the detailed content of How does php base64 encode and decode URL strings?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template