php手机号中间四位用星号*代替显示

  • 时间:
  • 浏览:17

通过PHP用三种简单的方法实现手机号中间四位(n位)用星号显示:


$mobile = '13912345678';

//1.字符串截取法

$newMobile1 = substr($mobile, 0, 5).'****'.substr($mobile, 9);

echo $newMobile1.'
';


//2.替换字符串的子串

$newMobile2 = substr_replace($mobile, '****', 5, 4);

echo $newMobile2.'
';


//3.用正则

$newMobile3 = preg_replace('/(\d{5})\d{4}(\d{2})/', '$1****$2', $mobile);

echo $newMobile3;