PHP隐藏电话号码/手机号码中间4位

PHP隐藏电话号码/手机号码中间4位

  /**
     * 1、隐藏电话号码中间4位和邮箱
     */
    function hidtel($phone)
    {
        //隐藏邮箱
        if (strpos($phone, '@')) {
            $email_array = explode("@", $phone);
            $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($phone, 0, 3); //邮箱前缀
            $count = 0;
            $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $phone, -1, $count);
            $rs = $prevfix . $str;
            return $rs;
        } else {
            //隐藏联系方式中间4位
            $Istelephone = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话
            if ($Istelephone) {
                return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone);
            } else {
                return preg_replace('/(1[0-9]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone);
            }
        }

    }

    /**
     * 2、使用hidtel
     */
    public function useHidel()
    {
        $phone1 =  $this->hidtel("010-57033050");
        $phone2 =  $this->hidtel("01057033050");
        $mobil = $this->hidtel("13661226666");
        $email = $this->hidtel("[email protected]");

        return   $phone1.'</br>'.$phone2.'</br>'.$mobil.'</br>'.$email;
    }

效果:

//正则判断是否是邮箱、手机号和用户账号  登录时验证

  $checkmail = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';//是否邮箱
        if(preg_match($checkmail,$userName)){
           $type = 'email';
           $value = $userName;
        }elseif(preg_match("/^1[34578]\d{9}$/", $userName)){ //是否手机号
            $type = 'mobile';
            $value = $userName;
        }else{
            $type = 'user_name';
            $value = $userName;
        }

猜你喜欢

转载自blog.csdn.net/qq_14969259/article/details/81165139