note 92980 modified in function.base64-decode by danbrown

From: Date: Tue, 18 Aug 2009 17:22:40 +0000
Subject: note 92980 modified in function.base64-decode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159607@lists.php.net to get a copy of this message
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. --was-- 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 $decoded = base64_decode($encoded); for big $encoded strings, it's saver to use $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. http://php.net/manual/en/function.base64-decode.php

« previous php.notes (#159607) next »