Backend Development 2 min read

PHP rawurlencode Function: Description, Parameters, and Examples

The article explains PHP's rawurlencode() function, detailing its RFC 3986‑based URL encoding behavior, signature, parameter description, return value, and provides two practical code examples demonstrating how to encode special characters in FTP and HTTP URLs.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rawurlencode Function: Description, Parameters, and Examples

The rawurlencode() function in PHP encodes a string according to RFC 3986, converting all non‑alphanumeric characters except -_. into percent‑encoded sequences.

Signature: string rawurlencode(string $str)

Parameters: $str – the URL or string to be encoded.

Return value: a string where characters other than -_. are replaced with % followed by two hexadecimal digits, preserving the original meaning of the URL.

Example 1:

<?php
echo '<a href="ftp://user:' . rawurlencode('foo @+%/') . '@ftp.example.com/x.txt">';
?>

Output:

<a href="ftp://user:foo%20%40%2B%25%[email protected]/x.txt">

Example 2:

<?php
echo '<a href="http://example.com/department_list_script/' . rawurlencode('sales and marketing/Miami') . '">';
?>

Output:

<a href="http://example.com/department_list_script/sales%20and%20marketing%2FMiami">
PHPExampleURL encodingrfc3986rawurlencode
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.