Skip to content

Instantly share code, notes, and snippets.

@gugglegum
Created March 18, 2019 05:47
Show Gist options
  • Save gugglegum/92d5e4226430194927f61f44dc6b34d1 to your computer and use it in GitHub Desktop.
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.)
<?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%40p@www.example.com/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