DevToolsForYou

URL Encoder and Decoder in PHP — Code Examples

URL Encoder and Decoder in PHPUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

PHP has urlencode() for form data (spaces as +) and rawurlencode() for URL path segments (spaces as %20). Use http_build_query() to build full query strings.

PHP
<?php

$input = "hello world & foo=bar";

// Form encoding (spaces as +)
$encoded = urlencode($input);
echo $encoded . "
"; // hello+world+%26+foo%3Dbar

// Decode
$decoded = urldecode("hello+world+%26+foo%3Dbar");
echo $decoded . "
"; // hello world & foo=bar

// RFC 3986 encoding (spaces as %20, for path segments)
$rawEncoded = rawurlencode($input);
echo $rawEncoded . "
"; // hello%20world%20%26%20foo%3Dbar

$rawDecoded = rawurldecode("hello%20world%20%26%20foo%3Dbar");
echo $rawDecoded . "
";

// Build a query string from an array
$qs = http_build_query(["q" => "hello world", "page" => 1]);
echo $qs . "
"; // q=hello+world&page=1
Notes & gotchas
  • urlencode() is for form data; rawurlencode() follows RFC 3986 and is for URL path components.
  • http_build_query() is the cleanest way to build a query string from an associative array.
  • urlencode() encodes spaces as +; rawurlencode() encodes them as %20.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages