- preg_replace: documentation ( source)
- json_encode: documentation ( source)
<?php
$ps = ["fo\"obar", "foobar\"", "foobar\\\\\"", "foo\\\"\"bar"];
$expect = '{"foo":{"password":"****"}}';
foreach ($ps as $p) {
$c = json_encode(["foo"=>["password"=>$p]]);
if ($expect !== preg_replace('{("password"\s*:\s*)"(?:[^"\\\\]|(?:\\\\.))*"}', '$1"****"', $c)) {
echo "Password $p is out of this league!";
exit(1);
}
}