Home >php教程 >php手册 >URL安全的字符串base64编码和解码

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

WBOY
WBOYOriginal
2016-06-07 11:45:531689browse

如果直接使用base64_encode和base64_decode方法的话,生成的字符串可能不适用URL地址。下面的方法可以解决该问题:
URL安全的字符串编码:function urlsafe_b64encode($string) {<br>    $data = base64_encode($string);<br>    $data = str_replace(array('+','/','='),array('-','_',''),$data);<br>    return $data;<br> }URL安全的字符串解码:function urlsafe_b64decode($string) {<br>    $data = str_replace(array('-','_'),array('+','/'),$string);<br>    $mod4 = strlen($data) % 4;<br>    if ($mod4) {<br>        $data .= substr('====', $mod4);<br>    }<br>    return base64_decode($data);<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

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