PHP user name/mobile phone number/IP Address indicates positioning the asterisk display-PHP source code

Source: Internet
Author: User
When I was a beginner in php, I knew to use the replacement function to replace the specified number. But we need to simply replace the specified position. Below I will introduce several examples to show the asterisk. When I was a beginner in php, I knew to use the replacement function to replace the specified number. But we need to simply replace the specified position. Below I will introduce several examples to show the asterisk.

Script ec (2); script

Example: User Name

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 = ''; www.111cn. Net

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

The Code is as follows:

$ Str = "aliyun.com ";
Echo cut_str ($ str, 1, 0). '**'. cut_str ($ str, 1,-1 );
// Output: for example, **

The following two IP addresses and mobile phone numbers are displayed with asterisks.

When I was a beginner in PHP, I saw someone else doing this by merging and splitting arrays! So turn it over !!

The Code is as follows:

$ Ip = $ _ SERVER ['remote _ ADDR '];

$ Ip_arr = explode ('.', $ ip );

$ Ip_arr [3] = '*';

$ Ip = implode ('.', $ ip_arr );

Echo $ ip;

?>

Mobile phone number

The Code is as follows:

$ Haoma = "15012345678 ";
Echo preg_replace ("/(d {3}) d {5}/", "$1 *****", $ haoma );
// 150 ***** 678
?>

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.