Some personal information would like to be confirmed by users or customers by displaying a part of the details. For example, visa card number, ID card number, email, phone number, etc. Sometimes only a few characters would be shown and other characters would be replaced by *. The followings are two examples to replace some characters by * in PHP.


The following example is to display 4 characters of the username of the email address. And the full domain name would be displayed. For example of, the output will be exam****

// Find the position of character @
$atpos = strpos($borrower['email'], '@');

// Get the first 4 characters of the email address
$display1 = substr($borrower['email'],0,4);

// Get the whole string after @
$display2 = substr($borrower['email'],$atpos);

// Count the length of replaced characters
$length = strlen($borrower['email']) - strlen($display1) - strlen($display2);

// Output pattern: xxxx****
echo $display1;
for($i=0; $i<$length; $i++)
       echo '*';
echo $display2;

Phone Number

It is much easier that only the first 4 characters of Phone Number would be displayed. Remark that the country code is ignored in this example. For example of phone number 98765432, the output is 9876****.

echo substr($borrower['phone'],0,4).'****';