Deprecated: pk_vd_gt_validate(): Implicitly marking parameter $args as nullable is deprecated, the explicit nullable type must be used instead in /home2/xzgzscom/public_html/xzgzs.com/wp-content/themes/puock/inc/fun/core.php on line 1290
php如何隐藏手机号中间四位(php隐藏手机号中间4位) - 小志工作室 php如何隐藏手机号中间四位(php隐藏手机号中间4位) – 小志工作室

php如何隐藏手机号中间四位(php隐藏手机号中间4位)

php隐藏手机号中间四位的方法:

/**
 * 隐藏手机号中间四位 17611155555 -> 176****5555
  * @param string $mobile 手机号
 * @return string
 */
function hide_mobile(string $mobile): string
{
    return substr_replace($mobile, '****', 3, 4);
}

三种实现方式

$tel = '12345678910';
//1.字符串截取法
$new_tel1 = substr($tel, 0, 3).'****'.substr($tel, 7);
var_dump($new_tel1);
//2.替换字符串的子串
$new_tel2 = substr_replace($tel, '****', 3, 4);
var_dump($new_tel2);
//3.用正则
$new_tel3 = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
var_dump($new_tel3);

执行结果:

string(11) “123****8910”

string(11) “123****8910”

string(11) “123****8910”

正文完
 0