PHP method to implement the function of hiding the middle four digits of mobile phone numbers

WBOY
Release: 2024-03-28 17:34:02
Original
270 people have browsed it

PHP method to implement the function of hiding the middle four digits of mobile phone numbers

PHP method to implement the function of hiding the middle four digits of mobile phone numbers

When developing a website or application, sometimes it is necessary to hide and protect the user’s mobile phone number User privacy. One common approach is to hide the middle four digits of a mobile phone number and only display the first three and last four digits. This article will introduce how to use PHP to achieve this function and provide specific code examples.

Implementation idea:

  1. First, get the mobile phone number entered by the user;
  2. Then, replace the middle four digits of the mobile phone number with *;
  3. Finally, output the hidden mobile phone number.

The following is a simple PHP function that implements the function of hiding the middle four digits of a mobile phone number:

function hidePhoneNumber($phoneNumber) { // 判断手机号码长度是否为11位 if(strlen($phoneNumber) != 11) { return "Invalid phone number"; } // 将手机号中间四位替换为* $hiddenNumber = substr($phoneNumber, 0, 3) . '****' . substr($phoneNumber, -4); return $hiddenNumber; } // 调用函数,隐藏手机号中间四位 $phoneNumber = '13812345678'; // 假设用户输入的手机号为13812345678 $hiddenPhoneNumber = hidePhoneNumber($phoneNumber); echo $hiddenPhoneNumber; // 输出隐藏后的手机号:138****5678
Copy after login

In the above code, thehidePhoneNumber()function Receive a mobile phone number as a parameter, first determine whether the length of the mobile phone number is 11 digits, then use thesubstr()function to replace the middle four digits of the mobile phone number with *, and finally return the hidden mobile phone number.

Using this function, you can easily implement the function of hiding the middle four digits of the mobile phone number during development, effectively protecting the user's private information. Hope the above code helps you!

The above is the detailed content of PHP method to implement the function of hiding the middle four digits of mobile phone numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!