PHP rawurlencode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, rawurlencode() replaces every unsafe character in a string with a % sign and two hex digits.

It follows RFC 3986, so a space becomes %20. Letters, digits, and the characters - _ . ~ are left alone.

Syntax

Call rawurlencode() with the values you need. Start with this line.

PHP
$safe = rawurlencode($text);
CallWhat it does
rawurlencode("a b")Returns "a%20b".
rawurlencode("a~b")Returns "a~b".

Use it for URL paths and for data in a URL. Use urlencode() for form-style query values.

Library declarations
PHP
rawurlencode(string $string): string

Parameters

ParameterDescription
stringRequired. The string to encode.

Return Value

Return ValueDescription
stringThe encoded string.

PHP Examples

Example 1 — Encode Text for a URL

Replace spaces and special characters with % codes.

PHP
<?php
$text = "Hello World & Co";

echo rawurlencode($text), PHP_EOL;

How It Works

  1. Start with plain text. It has spaces and an &.
  2. Call rawurlencode(). A space becomes %20.
  3. The & is encoded too. It becomes %26.
  4. Use the result in a URL. It is now safe.

Example 2 — Check Which Characters Stay

The tilde is kept, but a plus sign is encoded.

PHP
<?php
echo rawurlencode("a+b~c-d_e.f"), PHP_EOL;

How It Works

  1. Use a mix of characters. It has +, ~, -, _ and ..
  2. The + is encoded. It becomes %2B.
  3. The other four are kept. They are safe in a URL.
  4. Remember the rule. Letters, digits, - _ . ~ stay as they are.

Example 3 — Build a Download Link

Encode only the file name, then join it to the base URL.

PHP
<?php
$base = "https://example.com/files/";
$file = "My Report (2026).pdf";

echo $base . rawurlencode($file), PHP_EOL;

How It Works

  1. Keep the base URL as it is. Its : and / must not change.
  2. Encode the file name. Spaces and brackets are changed.
  3. Join the two parts. Use . to concatenate them.
  4. Print the link. It is safe to place in an href.

Frequently Asked Questions

rawurlencode() encodes a string so it can be placed safely in a URL. Unsafe characters become % followed by two hex digits.
rawurlencode(string $string): string. Pass the text and it returns the encoded text.
rawurlencode() writes a space as %20 and keeps ~. urlencode() writes a space as + and encodes ~.
Letters, digits, and the characters - _ . ~ are not encoded.
Not a whole URL. It would also encode the : and / characters. Encode each path piece or value on its own.

Next: urldecode()

urldecode() decodes a URL-encoded string and turns plus signs into spaces.

urldecode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful