Home >Backend Development >PHP Tutorial >PHP Programming Guide: A simple way to hide the middle four digits of a mobile phone number

PHP Programming Guide: A simple way to hide the middle four digits of a mobile phone number

PHPz
PHPzOriginal
2024-03-28 13:57:03807browse

PHP Programming Guide: A simple way to hide the middle four digits of a mobile phone number

PHP Programming Guide: A simple method to hide the middle four digits of a mobile phone number

In actual projects, sometimes we need to hide the mobile phone number Desensitization is performed, that is, the four digits in the middle of the mobile phone number are hidden to protect user privacy. In PHP programming, it is very simple to implement this function. Below we will introduce a method and give specific code examples.

First of all, we need to make it clear that a mobile phone number is usually a string of 11 digits. The effect we want to achieve is to display the first three and last four digits of the mobile phone number, and replace the middle four digits with . For example, "13812345678" is processed as "138*5678".

Next, we will use PHP's string processing function to implement this function. The specific code is as follows:

<?php
function hidePhoneNumber($phone)
{
    if(strlen($phone) != 11) {
        return "手机号格式不正确";
    }
    
    $start = substr($phone, 0, 3);
    $middle = "****";
    $end = substr($phone, -4);

    return $start.$middle.$end;
}

$phoneNumber = "13812345678";
$hiddenPhone = hidePhoneNumber($phoneNumber);
echo $hiddenPhone;
?>

In the above code, we define a function hidePhoneNumber, which accepts a mobile phone number as a parameter. First, we check whether the length of the mobile phone number is 11 digits. If not, it returns "The mobile phone number format is incorrect." Then we use the substr function to intercept the first three and last four digits of the mobile phone number, and replace the middle four digits with "**". Finally, the processed mobile phone number will be returned.

At the end of the code, we define an example mobile phone number $phoneNumber as "13812345678", and then call the hidePhoneNumber function to desensitize the mobile phone number. and output the result.

With this simple code example, we have implemented the function of hiding the middle four digits of a mobile phone number. In actual projects, this method can be expanded and improved according to needs to meet the needs of different scenarios.

I hope the above code is helpful to you, and happy coding!

The above is the detailed content of PHP Programming Guide: A simple way to hide the middle four digits of a mobile phone number. 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