base64_encode
-
In Ruby, we use the
Base64class to encode data using a base 64 representation. We first need to require theBase64library usingrequire "base64". This is similar to usingrequire_onceto include a class in PHP.To get the equivalent result of PHP’s
base64_encodefunction, we’ll use theBase64.encode64class method.PHP
$result = base64_encode('Hello world'); var_export($result); // => 'SGVsbG8gd29ybGQ='
Ruby
require 'base64' p Base64.encode64("Hello world") # => "SGVsbG8gd29ybGQ=\n"
Take note that the Ruby version inserts a newline at the end of the string where PHP does not
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

