Home  >  Article  >  Backend Development  >  How to hide the number of numbers in php

How to hide the number of numbers in php

藏色散人
藏色散人Original
2021-11-23 09:21:152001browse

php实现隐藏号码几位的方法:1、创建一个PHP示例文件;2、通过“function hidtel($phone){...}”方法隐藏电话号码和手机号码中间4位即可。

How to hide the number of numbers in php

本文操作环境:Windows7系统、PHP7.1、Dell G3电脑。

php怎么实现隐藏号码几位?

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(&#39;/([\d\w+_-]{0,100})@/&#39;, &#39;***@&#39;, $phone, -1, $count);
            $rs = $prevfix . $str;
            return $rs;
        } else {
            //隐藏联系方式中间4位
            $Istelephone = preg_match(&#39;/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i&#39;, $phone); //固定电话
            if ($Istelephone) {
                return preg_replace(&#39;/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i&#39;, &#39;$1****$2&#39;, $phone);
            } else {
                return preg_replace(&#39;/(1[0-9]{1}[0-9])[0-9]{4}([0-9]{4})/i&#39;, &#39;$1****$2&#39;, $phone);
            }
        }

    }

    /**
     * 2、使用hidtel
     */
    public function useHidel()
    {
        $phone1 =  $this->hidtel("010-57033050");
        $phone2 =  $this->hidtel("01057033050");
        $mobil = $this->hidtel("13661226666");
        $email = $this->hidtel("1963632356@qq.com");

        return   $phone1.&#39;</br>&#39;.$phone2.&#39;</br>&#39;.$mobil.&#39;</br>&#39;.$email;
    }

效果:

 

How to hide the number of numbers in php

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

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

 推荐学习:《PHP视频教程

The above is the detailed content of How to hide the number of numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn