Edit: base64_encode

Description: Encodes string using MIME base64 algorithm.
Edit tests
You have to be logged in to edit tests.


<<  <  >  >> Page 1 of 1: 1 Size: -10+10102050100Default
Subject From Date
: base64_encode version 460 John Elliot 2011-12-27 17:08
This function is a bit of a trick. The upstream developers at phpjs.org put in utf-8 encoding and decoding into the function. This is necessary to get the expected behaviour using JavaScript strings instead of PHP strings.

In PHP if you run

  strlen( '漢字仮名交じり文' )
You get 24. In JavaScript if you call

You get 8. The difference is in how the string is represented in memory. PHP uses binary strings, whereas it seems JavaScript is using a UTF-16 (or higher) representation. This means when you call charCodeAt in JavaScript you might get a value greater than 255.

Anyway, the UTF-8 encoding and decoding by the base64_* functions was creating a problem for me, because sometimes you're already dealing with a 'binary string' in JavaScript, which means the string shouldn't be UTF-8 encoded before base64 coding. So I've moved out the utf8 encoding, and if you want or need utf8 encoding you should do it yourself before passing a value to the base64_encode function.
<<  <  >  >> Page 1 of 1: 1 Size: -10+10102050100Default
Please Login or Register to post comments.