How to Partially Hide Phone in PHP?

How to Partially Hide Phone in PHP

In this tutorial, we will show you how to hide or mask the mobile number using PHP. Most of the time we require to hide the partial details which we do not want to show the end-user. And among those details, the phone number is one, so it’s basically functionally that we need to integrate into the web application as per requirement.

Sometimes we need to hide or mask the detail in the web application. In this tutorial, we will learn to hide partially mobile numbers using the PHP substr() function.

We have seen this type of example in many comment systems when users validate himself using mobile number and after that post, comment on the website, and when the website display that comment it most of the time does not show the mobile number adjacent to the comment but sometimes shows the masked mobile number. Another example we can take user panel, in which the user registered himself and after registration, the user can visit the profile, but I have seen many websites that keep hide or mask the mobile number of the user and give an option to show it if the user wants.

In the below code we have tried to explain the concept of masking the phone number with an easy example so that you can get it quickly. In another word, we can also say it replace the characters or number with asterisks in PHP using substr() function

PHP substr() Function

The substr() is a built-in function in PHP that is used to extract a part of a string by passing the string position values.


substr(string_name, start_position, string_length_to_cut)

Let’s understand mobile masking with the help of below example.


function mask_mobile_no($number)
    return substr($number, 0, 2) . '******' . substr($number, -2);

$number = 9876543210;

echo mask_mobile_no($number) ;




In the above example, we have created a function name mask_mobile_no() in which we are passing the mobile number. Inside the function, we have taken the first two numbers of the mobile using substr() function and the last two numbers with the same function. In last we just concatenated the first two numbers with 6 asterisks and the last 2 values of the mobile number, which we have extracted before.


We have tried to explain the mobile masking process with an easy example, hope you guys liked it. If you have any query please comment below. You can check How to How to Partially hide email address in PHP