rawurldecode

  • In Ruby, we use the CGI class to decode a URL. We first need to require the CGI library using require "cgi". This is similar to using require_once to include a class in PHP.

    To get the equivalent result of PHP’s rawurldecode function, we’ll use the CGI.unescape class method.

    PHP

    $result = rawurldecode('Hello%20%3Cb%3Ethere%21%3C%2Fb%3E');
    var_export($result);
    // => 'Hello <b>there!</b>'

    Ruby

    require 'cgi'
     
    p CGI.unescape("Hello%20%3Cb%3Ethere%21%3C%2Fb%3E")
    # => "Hello <b>there!</b>"

    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.