Example below replaces username and password with stars and leaves other parts intact.


Function


function obscureUriDetails($url)
{
return preg_replace('@(ftp|http)://.*:.*\@@', '$1://****:****@', $url);
}

Example


echo obscureUriDetails('http://username-goes-here:password-goes-here@drop.html/part/file.csv');
// http://*******:*******@drop.html/part/file.csv

echo obscureUriDetails('ftp://username-goes-here:password-goes-here@drop.html/part/file.csv');
// fpt://*******:*******@drop.html/part/file.csv

Json example


$json = '{
"username": "u",
"key_1": "hello",
"account_no": 44,
"inner": [
{
"password": "111111",
"name": "user_x",
"account_no": 33,
"parent": {
"sub": {
"account_no": 11,
"username": "user1",
"password": "123123",
"account_no": 22
}
}
}
],
"key_3": []
}';

$result = preg_replace(
[
'/\"account_no\": \d+/',
'/\"username\": \".*?\"/',
],
[
'"account_no": 0',
'"username": "hidden"',
],
$json
);

{
"username": "hidden",
"key_1": "hello",
"account_no": 0,
"inner": [
{
"password": "111111",
"name": "user_x",
"account_no": 0,
"parent": {
"sub": {
"account_no": 0,
"username": "hidden",
"password": "123123",
"account_no": 0
}
}
}
],
"key_3": [

]
}