URL encoder / decoder

Encode or decode URL strings using PHP's urlencode, rawurlencode, and base64. Parse any URL into its components and query parameters.

urlencode vs rawurlencode

Which one to use?

Use rawurlencode() for URL path segments and query values — it follows RFC 3986 and encodes spaces as %20. Use urlencode() only for form data submitted as POST bodies where spaces as + is expected.

Query strings

Building query strings

In PHP, use http_build_query($array) to build query strings correctly — it handles encoding automatically. Use parse_str($query, $output) to parse them back.

Common mistake

Double encoding

A common bug is encoding a value that's already encoded. The result has %25 instead of % in the output. Always decode before re-encoding if you're not sure of the current state.

Security

URL vs HTML encoding

URL encoding and HTML encoding are different things. Always use htmlspecialchars() when outputting user-supplied data in HTML, even if it came from a URL parameter.

Building PHP APIs or web applications?

URL handling, query strings, redirects, and HTTP — covered in our PHP developer ebooks and courses.

Browse ebooks → View courses →