PHP rawurldecode Function and Custom UTF-8 URL Decoding

This article explains PHP's rawurldecode() function for decoding URL‑encoded strings, shows its signature, parameters, return value, provides a simple usage example, notes UTF‑8 considerations, and presents a custom utf8RawUrlDecode() function for handling non‑standard %uXXXX sequences.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rawurldecode Function and Custom UTF-8 URL Decoding

PHP provides the rawurldecode() function to decode URL‑encoded strings, converting each “%xx” sequence back to its original character.

The function signature is string rawurldecode ( string $str ), where $str is the URL to decode, and it returns the decoded string.

Example usage:

<?php
echo rawurldecode('foo%20bar%40baz');
?>

The output is // foo bar@baz.

Note: the result of rawurldecode() (and urldecode()) is UTF‑8 encoded; if the page is not UTF‑8, you must convert the string before displaying.

When the URL contains non‑standard “%unnnn” sequences, the built‑in functions cannot decode them. The article provides a custom utf8RawUrlDecode($source) function that iterates through the string, handles Unicode “%uXXXX” sequences and escaped ASCII “%XX” sequences, builds the decoded string, and returns it.

The custom function uses substr(), hexdec(), chr(), and utf8_encode() to translate the encoded parts into proper characters.

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.

BackendPHPUTF-8rawurldecodeURL decodingcustom-function
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

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.