Craig Edmonds Craig Edmonds - 1 year ago 75
PHP Question

PHP: Detect password parameter in string and replace with default text

I have an api logging system which records logins but I do not want to store passwords in the logs.

This is an example of a request string to the log:

NOTE: the string will not be exactly the same and will contain parameters in different order, so I am thinking maybe someREGEX can handle this?

What I need to do, is:

  1. Detect if the parameter "password=" is in the string

  2. If its in the string replace the password part with OBFUSCATED so result will be:

I have tried this but does not work:
$request_string = preg_replace("/password=\d+/", "password=OBFUSCATED", $request_string);

Answer Source

Try this code, it works


$request_string = "";
echo $request_string = preg_replace("/password=\w+/", "password=OBFUSCATED", $request_string);

Output :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download