您现在的位置是:首页 > PHP实例

李清波 2019-12-02 PHP实例 1614 复制当前网址

PHP实现手机号码中间四位数替换成星号*

示例:

字符串截取法

/**
 * 中间加密 字符串截取法
 */
public static function encryptTel($tel) {
    $new_tel = substr($tel, 0, 3).'****'.substr($tel, 7);
    return $new_tel;
}


替换字符串

/**
 * 中间加密 替换字符串的子串
 */
public static function encryptTel($tel) {
    $new_tel = substr_replace($tel, '****', 3, 4);
    return $new_tel;
}


正则

/**
 * 中间加密 用正则
 */
public static function encryptTel($tel) {
    $new_tel = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
    return $new_tel;
}


文章来源:http://www.liqingbo.com/blog-1618.html

评论