屏蔽电话号码(无论长度)

  • 本文关键字:电话号码 屏蔽 php
  • 更新时间 :
  • 英文 :


我尝试了以下功能

str_repeat("*", strlen($value['number'])-2) . substr($value['number'], -2)

str_pad(substr($value['number'], -2), strlen($value['number']), '*', STR_PAD_LEFT)

然而,它们都产生了相同的结果:******43


我想要实现的是:70****43,但无论数字长度如何都是动态的。

有什么建议可以做吗?

见下文:

$num = '70564843';
echo substr( $num, 0, 2 ) // Get the first two digits
.str_repeat( '*', ( strlen( $num ) - 4 ) ) // Apply enough asterisks to cover the middle numbers
.substr( $num, -2 ); // Get the last two digits

输出:

70****43

这是一种动态:

$phoneNumber = 123456789; //your phone number
$showFirstDigits = 2; //how many digits to show in the beggining of the phone number
$showLastDigits = 2; // how many digits to show in the end of the phone number
echo(
substr_replace($phoneNumber,str_repeat('*',strlen($phoneNumber) - $showFirstDigits - $showLastDigits),
$showFirstDigits,
strlen($phoneNumber) - $showFirstDigits - $showLastDigits));

最新更新