Home > Backend Development > PHP Tutorial > PHP uses asterisks to hide some user names, ID cards, IPs, mobile phone numbers, etc._PHP tutorial

PHP uses asterisks to hide some user names, ID cards, IPs, mobile phone numbers, etc._PHP tutorial

WBOY
Release: 2016-07-13 10:34:27
Original
1025 people have browsed it

1. Imitate Taobao review and purchase records to hide part of the user name. The following code is available for personal testing.

Copy code The 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))
{
                                                                                                              ​            }
else
                                                                                                                 If(ord(substr($string, $i, 1))> ;129) $i++;
}
}
}<🎜 //if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
}
}
}

Usage example:



Copy code


The code is as follows:
$str = "Tathagata Palm";echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);//Output: like **palm
2. The last 4 digits of the PHP ID number are hidden with asterisks

A very simple question. I want to hide the 4 digits of the ID card number and birthday. I didn’t see it when I first searched for the function. Then I used several functions to handle it. I thought it was too troublesome so I searched online. Later I found a function. can be processed directly, so record it:
substr_replace() function introduction:


Copy code


The code is as follows:

Definition and Usagesubstr_replace() function replaces part of a string with another string. Syntaxsubstr_replace(string,replacement,start,length)
Parameter Description
string Required. Specifies the string to check.
replacement
Required. Specifies the string to be inserted.
start
Required. Specifies where in the string to begin replacement.
Positive numbers - Replace at start offset
Negative numbers - Replace at start offset from end of string
0 - Start at first character in string Replace
length
optional. Specifies how many characters to replace.
Positive number - the length of the string to be replaced
Negative number - the number of characters to be replaced from the end of the string
0 - insertion instead of replacement



Usage example:


Copy code

The code is as follows:

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

3. Replace the last digit of the IP with an asterisk

Replace the last digit of the IP with an asterisk and the code is as follows:
Method 1:

Copy the code The code is as follows:

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


Method 2:
Copy code The code is as follows:

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

4. Five ways to hide mobile phone numbers with * asterisks

Copy code The 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("/(1d{1,4})dddd(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 phone
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);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/750856.htmlTechArticle1. Imitate Taobao review and purchase records to hide part of the user name. The following code is available for personal testing. Copy the code The code is as follows: function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') { if...
Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template