PHP safe URL string base64 encoding and decoding

WBOY
Release: 2016-07-29 08:55:51
Original
976 people have browsed it

If you use the base64_encode and base64_decode methods directly, the generated string may not be suitable for URL addresses. The following method can solve this problem:

URL safe string encoding:

Copy the code The code is as follows:


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


URL Safe string decoding:

Copy code The code is as follows:


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

The above introduces the PHP secure URL string base64 encoding and decoding, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!