Home >Backend Development >PHP Tutorial >PHP安全的URL字符串base64编码和解码_PHP

PHP安全的URL字符串base64编码和解码_PHP

WBOY
WBOYOriginal
2016-06-01 11:50:58786browse

如果直接使用base64_encode和base64_decode方法的话,生成的字符串可能不适用URL地址。下面的方法可以解决该问题:

URL安全的字符串编码:
复制代码 代码如下:
function urlsafe_b64encode($string) {
   $data = base64_encode($string);
   $data = str_replace(array('+','/','='),array('-','_',''),$data);
   return $data;
 }


URL安全的字符串解码:
复制代码 代码如下:
function urlsafe_b64decode($string) {
   $data = str_replace(array('-','_'),array('+','/'),$string);
   $mod4 = strlen($data) % 4;
   if ($mod4) {
       $data .= substr('====', $mod4);
   }
   return base64_decode($data);
 }

Statement:
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