PHP base64_encode和*nix base64之間有什么區別

[英]what are the differences between PHP base64_encode and *nix base64


PHP's base64_encode is returning a different string to the linux base64 command. Why is this?

PHP的base64_encode將向linux base64命令返回另一個字符串。這是為什么呢?

PHP:

PHP:

$ php
<?php
echo base64_encode('test');
?>
dGVzdA==

Linux base64:

Linux base64:

$ echo 'test' | base64
dGVzdAo=

6 个解决方案

#1


42  

echo usually outputs a new line character at the end of the string, to suppress that use the -n switch:

echo通常在字符串末尾輸出一個新的行字符,用-n開關來抑制:

$ echo -n 'test' | base64
dGVzdA==

Similarly for PHP:

同樣為PHP:

$ php
<?php
echo base64_encode("test\n");
?>
dGVzdAo=

#2


11  

open console in your browser, type atob('dGVzdAo='):

在瀏覽器中打開控制台,輸入atob('dGVzdAo='):

http://gyazo.com/68d16f666c394887929408c519ac1b23.png

http://gyazo.com/68d16f666c394887929408c519ac1b23.png

You have extra character in your input. And that is 0x0A (LF).

你的輸入中有額外的字符。這是0x0A (LF)

#3


9  

When doing an echo it gives me this:

當做一個回聲時,它給了我這個:

MacPro:~ bardiir$ echo 'test'
test
MacPro:~ bardiir$ 

I'd guess you might have an included line-ending in the unix one as echo is probably appending a newline character even if you pipe it throuch to the base64 encode.

我猜您可能在unix中有一個包含的行結尾,因為echo可能會附加一個換行字符,即使您將它連接到base64編碼。

#4


2  

The linux base64 has a new line at the end.

linux base64在末尾有一個新行。

#5


2  

It is because Unix version encodes also the end of line.

這是因為Unix版本的代碼也是行尾。

To receive similar effect in PHP you will have to do something like that:

要在PHP中獲得類似的效果,您必須做以下事情:

echo base64_encode('test'.PHP_EOL);

which will output:

將輸出:

dGVzdAo=

See the proof here: ideone.com/HorVD

參見這里的證明:ideone.com/HorVD

EDIT: As Charles mentioned, PHP_EOL is platform-specific, so to check the above on Windows you will have to replace it with Unix-like end of line symbol:

編輯:正如Charles所提到的,PHP_EOL是特定於平台的,所以要在Windows上檢查上面的內容,你必須用類unix的結尾替換它:

echo base64_encode("test\n");

#6


0  

The same to above guys.

上面的人也是一樣。

In mac os X, just test this :

在mac os X中,測試如下:

$ echo  'test' | cat -e

test$

Or,

或者,

$ echo -n  'test' | cat -e

test

And,about the echo command, can see the tip:

並且,關於echo命令,可以看到提示:

echo -e option in ubuntu doesn't work

在ubuntu中echo -e選項不起作用


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/11/7251000ca6f0865d7a715a92ec6167aa.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com