Last active
March 30, 2026 18:31
-
-
Save stemar/3411cc8ce8fc98bf19116223b851ef43 to your computer and use it in GitHub Desktop.
Encode the query of a URL with RFC3986.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function encode_query(string $url): string { | |
| if ($query = parse_url($url, PHP_URL_QUERY)) { | |
| [$path] = explode($query, $url, 2); | |
| parse_str($query, $data); | |
| $query = http_build_query($data, "", NULL, PHP_QUERY_RFC3986); | |
| if ($fragment = parse_url($url, PHP_URL_FRAGMENT)) { | |
| $fragment = '#'.rawurlencode($fragment); | |
| } | |
| return join([$path, $query, $fragment]); | |
| } | |
| return $url; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.