为什么base64_en code()添加斜线和QUOT; /"在结果? [英] Why is base64_encode() adding a slash "/" in the result?
问题描述
我在我的编码应用程序的URL后缀:
$网址='?科目_D = 1;
回声base64_en code($网址);//输出
c3ViamVjdHM / X2Q9MQ ==
在X2请注意斜线。
这是怎么回事?我以为只的base64输出A-Z,0-9和'='作为填充?我一直在使用一个在线的base64连接codeR检查试过了,而且似乎永远的base64做到这一点。我不知道它是下划线_或问号?或=吧?
没有。该Base64编码字母包括A-Z,A-Z,0-9和 +
和 /
。
您可以替换他们,如果你不关心对其他应用程序的可移植性。
请参阅:<一href=\"http://en.wikipedia.org/wiki/Base64#Variants_summary_table\">http://en.wikipedia.org/wiki/Base64#Variants_summary_table
您可以使用类似这些使用自己的符号来代替(替换 -
和 _
通过任何你想要的的,只要它是不是在BASE64基字母表,当然!)。</ p>
下面的示例将正常的base64到base64url 在RFC 4648规定:
功能base64url_en code($ S){
str_replace函数返回(阵列('+','/'),阵列(' - ','_'),base64_en code($ S));
}功能base64url_de code($ S){
返回base64_de code(str_replace函数(阵列(' - ','_'),阵列('+','/'),$ S));
}
I am encoding the URL suffix of my application:
$url = 'subjects?_d=1';
echo base64_encode($url);
// Outputs
c3ViamVjdHM/X2Q9MQ==
Notice the slash before 'X2'.
Why is this happening? I thought base64 only outputted A-Z, 0-9 and '=' as padding? I have tried using an online base64 encoder to check, and it seems base64 always does this. I can't tell if it's the underscore "_" or the question mark "?" or the "=" perhaps?
No. The Base64 alphabet includes A-Z, a-z, 0-9 and +
and /
.
You can replace them if you don't care about portability towards other applications.
See: http://en.wikipedia.org/wiki/Base64#Variants_summary_table
You can use something like these to use your own symbols instead (replace -
and _
by anything you want, as long as it is not in the base64 base alphabet, of course!).
The following example converts the normal base64 to base64url as specified in RFC 4648:
function base64url_encode($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
function base64url_decode($s) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
这篇关于为什么base64_en code()添加斜线和QUOT; /&QUOT;在结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!