How to hide the last two digits of an IP address in php as asterisks

Source: Internet
Author: User
This article describes how to hide the last two digits of an IP address in php as asterisks. it is a practical technique to use the regular expression matching method, for more information about how to hide IP addresses in php, see the example below. Share it with you for your reference. The specific implementation method is as follows:

In many public websites, the following IP segments are displayed as asterisks when the user's IP address is displayed. This protects user privacy, if you are interested, you can take a look.

Php regular format IP address, hide the last one.
Example

The code is as follows:

<? Php
// Hide the last digit
Return preg_replace ('/(\ d + )\. (\ d + )\. (\ d + )\. (\ d +)/is ', "$1. $2. $3. * ", $ ip );

// The last number of hidden IP addresses is *
Echo ereg_replace ("[^ \.] {1, 3} $", "*", $ ip );
?>


Example
How to hide the last or last segment of an IP address in php

The code is as follows:

<? Php
// -- Hide the last few IP addresses
$ Ip = '1970. 0.0.1 ';
$ Reg1 = '/((? : \ D + \.) {3}) \ d + /';
$ Reg2 = '~ (\ D +) \. (\ d + )~ ';
Echo preg_replace ($ reg1, "\ 1 *", $ ip); // The preceding output is 127.0.0 .*
Echo "------------------
";
Echo preg_replace ($ reg2, "$1. $2. *. *", $ ip); // The output result is 127. 0 .*.*
?>


Example

The code is as follows:

Function suohao ($ phone ){
$ P = substr ($ phone,). "*****". substr ($ phone );
Return $ p;
}


Of course, we can replace arrays 2 and 3 with arrays separated by commas (,) or combine arrays 0 and 1.

I hope this article will help you with php programming.

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.