php - 屏蔽电话号码,无论长度如何

I've tried the following functions

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

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

However they both yield the same result: ******43


What I am tying to achieve is this: 70****43, but dynamically regardless of number length.

Any suggestions how it can be done?

See below:

$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

Output:

70****43

It's kind of dynamic:

$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));