Hide the last 4 numbers of a string [closed]

4

I have a string of the value 187.10.61.291 , I want a function that takes the last 4 numbers and transforms it into * .

Example of expected result:

  

192.1**.*.* or 192.16*.**.* or 192.168.***.* or 192.168.**.**

How can I do this?

    
asked by anonymous 30.09.2016 / 04:45

5 answers

13

Another option is to use strrev() to invert the string or be 192.168.39.134 to 431.93.861.291 and apply a regular expression to replace the first 4 numbers that limit is set in the fourth argument of preg_replace() and last one more call strrev() to return the string to the 'original state'.

$str = strrev(preg_replace('/\d/', '*',  strrev('192.168.39.134'), 4));

Examples

192.168.0.1
192.1**.*.*

192.168.254.1
192.168.***.*

192.168.25.12
192.168.**.**

192.168.25.123
192.168.2*.***
    
30.09.2016 / 15:24
6

Here's a solution with basic string operations:

substr( $ip, 0, strrpos( $ip, '.' ) ).'.***';

See working at IDEONE

If you really want to change 4 digits:

substr( $ip, 0, strrpos( $ip, '.' ) - 1 ).'*.***';

See working at IDEONE

If you need the function to ignore malformed IPs, you can use a if :

function masked_ip( $ip ) {
    if( substr_count( $ip, '.' ) < 3 ) return $ip; // ou return '***.***.***.***';
    return substr( $ip, 0, strrpos( $ip, '.' ) - 1 ).'*.***';
}


Alternatives to explode :

Based on a comment from @rray in chat, it follows a version with explode :

For 4 digits:

function masked_ip( $ip ) {
    $ocs = explode( '.', $ip );
    $ocs[2]{strlen($ocs[2])-1} = '*';
    $ocs[3]='***';
    return implode( '.', $ocs );
}

See working at IDEONE

For 3 digits:

function masked_ip( $ip ) {
    $ocs = explode( '.', $ip );
    $ocs[3]='***';
    return implode( '.', $ocs );
}
    
30.09.2016 / 16:56
5

If you want it to consider any number, before and after a point you can use a for with working the string in a similar way to a vector:

<?php

function maskIp($value) {
    $len = strlen($value);
    $j = 0;

    for ($i = $len - 1; $j < 4 && $i > -1; --$i) {
        if (ctype_digit($value{$i})) {
            ++$j;
            $value{$i} = '*';
        }
    }

    return $value;
}

echo maskIp('127.0.0.100'), '<br>';
echo maskIp('127.0.0.10'), '<br>';
echo maskIp('127.0.0.1'), '<br>';
echo maskIp('127.0.255.100'), '<br>';
echo maskIp('127.0.25.100'), '<br>';

Example link

Another suggestion (before editing the question) with regex would be something like (this is an example to understand regex):

function maskIp($value) {
    $re  = '\d\.\d{3}|';    //Checa terminado com 5.255 por exemplo
    $re .= '\d{2}\.\d{2}|'; //Checa terminado com 55.125 por exemplo
    $re .= '\d{3}\.\d{1}';  //Checa terminado com 255.1 por exemplo

    return preg_replace('/(' . $re . ')$/', '*.***', $value);
}

echo maskIp('192.168.100.100'), PHP_EOL;
echo maskIp('192.168.100.10'), PHP_EOL;
echo maskIp('192.168.100.1'), PHP_EOL;

Following the idea of @rray a bit with @Bacco's idea of interpreting numbers 1 or 2 numbers after the dot is 3 asterisks as well.

Simplifying the example:

function maskIp($value) {
    return preg_replace('/('\d\.\d{3}|d{2}\.\d{2}|\d{3}\.\d{1})$/', '*.***', $value);
}

echo maskIp('192.168.100.100'), PHP_EOL;
echo maskIp('192.168.100.10'), PHP_EOL;
echo maskIp('192.168.100.1'), PHP_EOL;
    
30.09.2016 / 17:05
4

Basically this is the code for what you want

$new = substr("187.10.61.291", 0, -4) . 'xxxx';

The return of this is

187.10.61xxxx
    
30.09.2016 / 05:22
4

The question implies that you need to delete the last 3 numbers after the last point. That is, the last 4 characters of IP must be .*** .

So, I would do it this way:

preg_replace('/\d+$/', '***', '192.168.1.122')

The expression \d+ will only capture numeric values. The $ character is reporting that only expressions that end with \d (digits).

Update

If you want to capture the last 4 numbers and turn it into * , ignoring . , I suggest using the following code:

preg_replace('/\d{1}\.\d+$/D', '*.***', '192.168.100.122')
preg_replace('/\d{1}\.\d+$/D', '*.***', '192.168.1.100')

Result:

192.168.10*.***
192.168.*.***

Using preg_replace_callback .

I've also been able to work out a way to capture the last 4 digits, considering that the . character must remain.

See:

$replacer = function ($m) {
    return str_repeat('*', strlen($m[1])) . '.' . str_repeat('*', strlen($m[2]));
};

$result = preg_replace_callback('/(\d{1})\.(\d{1,3})$/', $replacer, '192.468.1.114');

See the example of preg_replace_callback in IDEONE

    
30.09.2016 / 15:37