PHP with asterisks to hide some of the user name, ID, IP, mobile phone number instance _php instance

Source: Internet
Author: User
Tags ord strlen

A, imitation Taobao comments purchase records hidden part of the user name, the following code is available.

Copy Code code 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;
}
}


Use examples:
Copy Code code as follows:
$STR = "The Buddha's Palm";
Echo Cut_str ($str, 1, 0). ' * * * CUT_STR ($STR, 1,-1);
Output: such as * * palm


Second, PHP ID number 4 digits hidden with the asterisk

A very simple question, want to ID card number of the birthday of the 4-bit hidden, a start to check the function incredibly did not see, and then used several functions to deal with, feel too troublesome on the internet search, and later found that a function can be directly processed, so record:
Introduction to the Substr_replace () function:

Copy Code code as follows:

Definitions and usage
The Substr_replace () function replaces part of a string with another string.
Grammar
Substr_replace (String,replacement,start,length)
Parameter description
String required. Specify the string to check.
Replacement
Necessary. Specify the string to insert.
Start
Necessary. Specify where the string begins to be replaced.
Positive number-replace at start offset
Negative number-replaces the start offset at the end of the string
0-Start the substitution at the first character in the string
Length
Optional. Specify how many characters to replace.
Positive number-The length of the string being replaced
Negative numbers-the number of replaced characters starting at the end of the string
0-Insert rather than replace

Use instance:

Copy Code code as follows:

[Code]
echo strlen ($idcard) ==15?substr_replace ($idcard, "* * *", 8,4):(strlen ($idcard) ==18?substr_replace ($idcard, "* * *", 10,4): "ID number is not normal!" ");

[/code]

Third, replace the IP last one with an asterisk

Replace the last IP with the asterisk code as follows:
Method One:

Copy Code code as follows:

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

Method Two:
Copy Code code as follows:

<?php
$ip =$_server[' remote_addr '];
$ip _arr= explode ('. ', $IP);
$ip _arr[3]= ' * ';
$ip = Implode ('. ', $ip _arr);
Echo $ip;
?>

Four, mobile phone number in the middle with * asterisk hidden method Five

Copy Code code as follows:
Method One
function Mobile_asterisk ($mobile)
{
$mobile _asterisk = substr ($mobile, 0,4). " ". substr ($mobile, 8, 3);
return $mobile _asterisk;
}
Echo Mobile_asterisk ("15810904579");
Method Two
echo Preg_replace ("/(1\d{1,4}) \d\d\d\d (\d{3,4})/", "\$1****\$2", "15810904579");

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

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

Method Five
Four digits in the middle of a masked phone number
function Hidtel ($phone)
{
$IsWhat = Preg_match ('/(0[0-9]{2,3}[\-]?[ 2-9][0-9]{6,7}[\-]? [0-9]?) /I ', $phone); Fixed telephone
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, the verification of the identity card number of the site also provides identity card attribution of the query tools are as follows:

Http://tools.jb51.net/bianmin/sfz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.