PHP with asterisks to hide some of the user name, ID, IP, mobile phone number and other examples

Source: Internet
Author: User
Tags join ord strlen

  This article mainly introduces PHP with asterisks to hide some of the user name, ID, IP, mobile phone number and other examples, the need for friends can refer to the following

A, imitation Taobao comments purchase records hidden part of the user name, the following code is available.     Code as follows: function Cut_str ($string, $sublen, $start = 0, $code = ' UTF-8 ') {    if ($code = = ' UTF-8 ') &nb Sp   {        $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       &NB Sp $sublen = $sublen *2;         $strlen = strlen ($string);         $TMPSTR = ';           for ($i =0 $i < $strlen; $i + +)         {  &nbsp         if ($i >= $start && $i < ($start + $sublen))             {                Ord (substr ($string, $i, 1) >129)       &NB Sp         {                    $tmpstr. = substr ($stri Ng, $i, 2);                                 Else &NB Sp               {                    $ tmpstr.= substr ($string, $i, 1);                            }             if (Ord (substr ($string, $i, 1) >129) $i + +;        }        //if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";         RetuRN $tmpstr;    }   Use example: The code is as follows: $str = "The Buddha's Palm"; Echo Cut_str ($str, 1, 0). ' * * * CUT_STR ($STR, 1,-1); Output: such as * * Palm     II, PHP ID number 4 After the use of the asterisk hidden   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 Later found that a function can be directly processed, so record: Substr_replace () Introduction:   Code as follows: definition and usage the Substr_replace () function replaces part of a string with another string. The syntax Substr_replace (string,replacement,start,length) parameter describes string     required. Specify the string to check. replacement      required. Specify the string to insert. start  required. Specify where the string begins to be replaced. Positive number-replace negative at start offset-replace 0 at start offset from end of string-length  optional at first character in string. Specify how many characters to replace. Positive number-replaced string length negative numbers-replaced characters from the end of the string 0-insert instead of replace     usages:     code is as follows: [Codes] echo strlen ($idcard) ==15?subs Tr_replace ($idcard, "* * *", 8,4):(strlen ($idcard) ==18?substr_replace ($idcard, "* * *", 10,4): "The ID number is not normal!" "); [/code]   III, replace the last IP with an asterisk   replace the last IP with an asterisk code as follows: Method one:     code is as follows: <?php str = ' 1.1.1.1 '; reg = '/((?:d +.) {3}) d+/'; Echo preg_replace (Reg, "1*", str);?> method Two:   code as follows: <?php  $ip =$_server[' remote_addr '];   $ip _arr= explode ('. ', $IP);   $IP _arr[3]= ' * ';   $IP = Implode ('. ', $ip _arr); echo $ip;?>     IV, the method of hiding with * asterisk in the middle of mobile phone number five     code as follows://Method One function Mobile_asterisk ($mobile) {  $mobil E_asterisk = substr ($mobile, 0,4). " ". substr ($mobile, 8, 3);  return $mobile _asterisk; } Echo Mobile_asterisk ("15810904579"); Method two echo Preg_replace ("/(1d{1,4}) dddd (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//mask four digit function Hidtel in the middle of telephone number ($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);    }}

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.