Created
March 18, 2019 05:47
-
-
Save gugglegum/92d5e4226430194927f61f44dc6b34d1 to your computer and use it in GitHub Desktop.
Replace password in URL to "******" if present (prepare secret URL for publishing in logs, reports, etc.)
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 | |
$username = 'John'; | |
$password = '%3q~7:rN@p'; | |
$url = 'http://' . urlencode($username) . ':' . urlencode($password) . '@www.example.com/path/to/file.txt'; | |
echo "Original URL: {$url}\n"; | |
echo "Hidden password URL: " . hidePasswordFromUrl($url) . "\n"; | |
/** | |
* Replaces password in URL to "******" (if present) | |
*/ | |
function hidePasswordFromUrl(string $url): string | |
{ | |
return preg_replace('|(^\w+://[^:]+:)(.+)@|', '\\1******@', $url); | |
} | |
// Outputs: | |
// | |
// Original URL: http://John:%253q%7E7%3ArN%[email protected]/path/to/file.txt | |
// Hidden password URL: http://John:******@www.example.com/path/to/file.txt | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment