SoFunction
Updated on 2025-03-03

PHP uses asterisk to hide some usernames, ID cards, IPs, mobile phone numbers, etc.

1. Imitate Taobao comment purchase record to hide some user names, and the following code is available for personal testing.

Copy the codeThe code is as follows:
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
    if($code == 'UTF-8')
    {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa, $string, $t_string);

        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
        return join('', array_slice($t_string[0], $start, $sublen));
    }
    else
    {
        $start = $start*2;
        $sublen = $sublen*2;
        $strlen = strlen($string);
        $tmpstr = '';

        for($i=0; $i< $strlen; $i++)
        {
            if($i>=$start && $i< ($start+$sublen))
            {
                if(ord(substr($string, $i, 1))>129)
                {
                    $tmpstr.= substr($string, $i, 2);
                }
                else
                {
                    $tmpstr.= substr($string, $i, 1);
                }
            }
            if(ord(substr($string, $i, 1))>129) $i++;
        }
        //if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
        return $tmpstr;
    }
}


Example of usage:
Copy the codeThe code is as follows:
$str = "Tathagata's Divine Palm";
echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);
//Output: like **Palm


2. The last 4 digits of PHP ID number are hidden with an asterisk.

A very simple question is that I want to hide the 4-digit birthday of the ID card. At the beginning, I didn’t see the function. Then I used several functions to process it. I thought it was too troublesome and searched online. Later, I found that there was a function that could be processed directly, so I recorded it:
Introduction to the substr_replace() function:

Copy the codeThe code is as follows:

Definition and usage
The substr_replace() function replaces part of the string with another string.
grammar
substr_replace(string,replacement,start,length)
Parameter Description
string      Required. Specifies the string to be checked.
replacement 
Required. Specifies the string to be inserted.
start 
Required. Specifies where to start the replacement of the string.
Positive number - start replacing at the start offset
Negative number - replace at the start offset at the end of the string
0 - Start replacing at the first character in the string
length 
Optional. Specify how many characters to replace.
Positive number - replaced string length
Negative number - the number of replaced characters starting from the end of the string
0 - Insert rather than replace

Examples of usage:

Copy the codeThe code is as follows:

[code]
echo strlen($idcard)==15?substr_replace($idcard,"****",8,4):(strlen($idcard)==18?substr_replace($idcard,"****",10,4):"The number of ID card digits is abnormal!");

[/code]

3. Replace the last bit of IP with an asterisk

Replace the last bit of IP with an asterisk. The code is as follows:
Method 1:

Copy the codeThe code is as follows:

<?php
str = '1.1.1.1';
reg = '/((?:\d+\.){3})\d+/';
echo preg_replace(reg, "\\1*", str);
?>

Method 2:
Copy the codeThe code is as follows:

<?php
$ip =$_SERVER['REMOTE_ADDR'];
  $ip_arr= explode('.', $ip);
  $ip_arr[3]='*';
  $ip= implode('.', $ip_arr);
echo $ip;
?>

4. Five methods to hide with * asterisk in the middle of your mobile phone number

Copy the codeThe code is as follows:
//Method 1
function mobile_asterisk($mobile)
{
 $mobile_asterisk = substr($mobile,0,4)."****".substr($mobile,8,3);
 return $mobile_asterisk;
}
echo mobile_asterisk("15810904579");
//Method 2
echo preg_replace("/(1\d{1,4})\d\d\d\d(\d{3,4})/", "\$1****\$2", "15810904579");

//Method 3
$haoma="15012345678";
echo preg_replace("/(d{3})d{5}/","$1*****",$haoma);
//Output 150***678

//Method 4
$tel1 = "13888111188";
$tel2 = "+8613888111188";
$tel3 = "0861088111188";
$tel4 = "086-010-88111188";
echo preg_replace('/(^.*)\d{4}(\d{4})$/','\\1****\\2',$tel1),"\n";
echo preg_replace('/(^.*)\d{4}(\d{4})$/','\\1****\\2',$tel2),"\n";
echo preg_replace('/(^.*)\d{4}(\d{4})$/','\\1****\\2',$tel3),"\n";
echo preg_replace('/(^.*)\d{4}(\d{4})$/','\\1****\\2',$tel4),"\n";

//Method 5
//Shield the four digits in the middle of the phone number
function hidtel($phone)
{
$IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //Landline
    if($IsWhat == 1)
    {
        return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);

    }
    else
    {
        return  preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
    }
}

In addition, this site also providesIdentity card home query toolas follows:

http://tools./bianmin/sfz