Home > Article > Backend Development > PHP secure URL string base64 encoding and decoding example code
This article mainly introduces PHPSecurityURLStringbase64 encoding and decoding. Some unsafe characters are replaced on the basis of base64. Friends in need can refer to it. Next
If you use the base64_encode and base64_decode methods directly, the generated string may not be suitable for the URL address. The following method can solve this problem:
URL safe string encoding:
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:
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 is the detailed content of PHP secure URL string base64 encoding and decoding example code. For more information, please follow other related articles on the PHP Chinese website!