rawurlencode
-
In Ruby, we use the
CGIclass to encode a URL. We first need to require theCGIlibrary usingrequire "cgi". This is similar to usingrequire_onceto include a class in PHP.To get the closest equivalent of the result of PHP’s
rawurlencodefunction, we’ll use theCGI.escapeclass method.The
rawurlencodefunction in PHP encodes according to the RFC 1738 spec, and
uses%20to encode spaces instead of a plus (+) character. If you want to encode your URLs according to RFC 1738, you’ll need to useString#gsubto replace the+character with%20.PHP
$result = rawurlencode("Hello <b>there!</b>"); var_export($result); // => 'Hello%20%3Cb%3Ethere%21%3C%2Fb%3E'
Ruby
require 'cgi' p CGI.escape("Hello <b>there!</b>").gsub("+", "%20") # => "Hello%20%3Cb%3Ethere%21%3C%2Fb%3E"
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.

