在请求 header 中找到用户名和密码并感应它

Find the username and password in a request header and sensor it

我收到了来自第三方的完整请求 header。我需要隐藏密码和用户名。

Php-Auth-Pw:     p@ssword
Php-Auth-User:   testuser

我怎样才能让它看起来像这样

Php-Auth-Pw:     ******
Php-Auth-User:   ******

我还没有完全理解正则表达式,我刚刚阅读了一些关于回顾的内容。

我找到了这个;

(?<=\Php-Auth-Pw:\s)(\w+)
(?<=\Php-Auth-User:\s)(\w+)

但是使用 PHP 风味的正确方法是什么?

您只需调用 preg_replace:

即可替换它们
$s = preg_replace('~\bPhp-Auth-(?:Pw|User):\s*\K\S+~', '******', $s);

regex demo

详情:

  • \b - 前导词边界
  • Php-Auth- - 文字字符序列 Php-Auth-
  • (?:Pw|User) - PwUser 子串
  • : - 冒号
  • \s* - 0+ 个空格
  • \K - 匹配重置运算符
  • \S+ - 1 个或多个非空白字符