Using PHP urlencode() and urldecode() Functions for URL Encoding and Decoding

This article explains how PHP's urlencode() and urldecode() functions work, provides their syntax and practical code examples, and briefly mentions related functions like rawurlencode() and rawurldecode() for handling special characters in URLs.

php Courses
php Courses
php Courses
Using PHP urlencode() and urldecode() Functions for URL Encoding and Decoding

In PHP, URL encoding and decoding are common operations that convert special characters in a URL to encoded values and back to their original form.

PHP offers a set of functions for these tasks; this article introduces the frequently used urlencode() and urldecode() functions and provides code examples.

1. urlencode() function

The urlencode() function encodes a string for use in a URL. Its syntax is: string urlencode(string $str) Example:

$url = "https://www.example.com/index.php?id=123&name=John Doé";
$encoded_url = urlencode($url);
echo $encoded_url;

// Output: https%3A%2F%2Fwww.example.com%2Findex.php%3Fid%3D123%26name%3DJohn+Do%C3%A9

The example shows a URL containing special characters being encoded, with the characters converted to their corresponding encoded values.

2. urldecode() function

The urldecode() function decodes a URL-encoded string back to its original characters. Its syntax is: string urldecode(string $str) Example:

$encoded_url = "https%3A%2F%2Fwww.example.com%2Findex.php%3Fid%3D123%26name%3DJohn+Do%C3%A9";
$decoded_url = urldecode($encoded_url);
echo $decoded_url;

// Output: https://www.example.com/index.php?id=123&name=John Doé

The example demonstrates decoding an encoded URL, restoring the original special characters.

Besides urlencode() and urldecode(), PHP also provides rawurlencode() and rawurldecode(), which function similarly but handle certain characters slightly differently.

Using these URL encoding and decoding functions ensures that special characters in URLs are processed correctly, preventing broken links or query parameters and improving security and accuracy in web development.

For further learning, the article includes promotional links to various programming study material collections (Java, C, Front‑end, PHP, etc.).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendweb-developmenturlencodeurldecodeurl-encoding
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

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.