PHP base64_decode编码的数据进行解码

2015-04-13 分类:js,php,html, php 阅读(9290) 评论(3)

base64_decode — 对使用 MIME base64 编码的数据进行解码

说明
string base64_decode ( string $encoded_data )
base64_decode() 对 encoded_data 进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的。

Example #1 base64_decode() 示例

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>

此示例将显示:

This is an encoded string

笔记
PHP base64_decode note #1
base64_decode seems to fail when decoding big files/strings. I had an issue decoding a 7MB image file. Here is a solution that worked for me:

$decodedstring=base64_decode(chunk_split($encodedstring));
PHP base64_decode note #2
If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

<?php
  $encodedData = str_replace(' ','+',$encodedData);
  $decocedData = base64_decode($encodedData);
?>

merci..........
PHP base64_decode note #3
If you want to save data that is derived from a Javascript canvas.toDataURL() function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

<?php
  $encodedData = str_replace(' ','+',$encodedData);
  $decocedData = base64_decode($encodedData);
?>

PHP base64_decode note #4
I had some trouble trying to let base64_decode decode base64-strings longer than ~5k chars.

The base64-decoding function is a homomorphism between modulo 4 and modulo 3-length segmented strings. That motivates a divide and conquer approach: Split the encoded string into substrings counting modulo 4 chars, then decode each substring and concatenate all of them.

Then instead of

<?php $decoded = base64_decode($encoded); ?>

for big $encoded strings, it's saver to use

<?php 
$decoded = ""; 
for ($i=0; $i < ceil(strlen($encoded)/256); $i++) 
   $decoded = $decoded . base64_decode(substr($encoded,$i*256,256)); 
?>

where 256 can be replaced by a sufficiently small modulo 4 natural.

1 2 3 4

您可能也喜欢:

3种方法实现网页自动刷新重新加载

有时候我们需要网页自动刷新,比如用户通过Ajax加载了一些东西,而此时我们通过JavaScript无法完成网页内容的修改,那么我们就需要网页自动刷新。 大体来说,网页自动刷新有2中方法,一种是HTML的Meta标签,另外一种就是通过我们的JavaScript脚本,他们各有各的优势和缺点,这里分享给大...

more

防F12扒代码:按下F12关闭当前页面

只要按下那神秘的审查元素、按下那神秘的F12键,奇迹出现了!网站变成了空白页,可谓杀Bug放火必备佳品! 一段JavaScript代码即可实现上述功能,插入到 footer.php 的 前:  <script>function fuckyou(){ window.close(); /...

more

网站变灰代码-悼念遇难同胞

如果你也想和我一样网站变灰来悼念遇难的同胞 以下css代码加在你的css样式文件里面就可以了 html { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -m...

more

欢迎新朋友你的到来!
已经有3 条评论抢在你前面了~
昵称
邮箱
网站

  1. xyayy
    爱在发烧 官方

    This is a great idea. 这只是一个测试

    #1
    回复
  2. 小白

    留下脚印 有时间过来补

    #2
    回复
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

    - 00:00 / 00:00