Hide the last 4 numbers from a string

Asked

Viewed 2,081 times

4

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

Example of expected result:

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

How can I do that?

  • 2

    what would these ips look like? 192.168.0.1 - 192.168.10.1 - 192.168.100.1 - 192.168.10.10

  • 2

    192.1**.. - 192.16*.. - 192.168.. - 192.168..*

  • 1

    It’s a little confusing the question: "four last numbers", but it’s not just 3 last? or did you mean "4 last characters"?

  • 1

    4 last numbers, for example: 187.10.6*.***

5 answers

14


Another option is to use strrev() to invert the string or 192.168.39.134 flipped 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 finally another call from 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*.***
  • Nor do I understand why -1. The answer is correct, I will reset +1.

  • It was not me who gave the negative, nor do I think that justifies, but I think that using 2 strrev is to kill the ant in basuka.

  • @rray understood the idea of Strrev. It is because of the character substitution limitation, isn’t it? It seems that PHP has a limitation on this.

  • @Wallacemaxters I don’t know a simple way to replace only the last 4 IP numbers so the strrev() 'plays them in the beginning, so I can force the number of replacements with preg_replace().

  • I think this solution is the one that perfectly reaches the result desired by the AP :)

6

Follows solution with basic operations of string:

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

See working on IDEONE

If you really want to change 4 digits:

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

See working on IDEONE

If you need the function to ignore poorly formed 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 with explode:

Based on a comment from @rray in chat, follow 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 on IDEONE

For 3 digits:

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

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 http://ideone.com/EyyOxD

Another suggestion (before editing the question) with regex would be something like (this is an example to understand the 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 @rray’s idea a little with @Bacco’s idea of interpreting numbers 1 or 2 after the point as 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;
  • 1

    +1 I couldn’t understand, but now I see why you did a "three there, one here".

  • @Wallacemaxters I was going to do everything together, but divided pro AP understand the Regex :)

4

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

Then I’d do it that way:

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

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

Updating

If you want to capture the last 4 numbers and turn it into *, ignoring the ., 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')

Upshot:

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

Using preg_replace_callback.

I was also able to work out a way to capture the last four digits, considering the character . must remain.

Behold:

$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');

Take the example of preg_replace_callback in the IDEONE

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
  • Has the substr stitch too? Or some function that does this?

  • @STRILEXLIVE... just add a dot there before the first "x".

  • 2

    Although the answer was beneficial, it seems to me that the intention of the PA was different. Using substr it will "cut" any value, regardless of whether it is a number or a point and place x at the end. AP left this in the comments: 192.1**.*.* - 192.16*.**.* - 192.168.***.* - 192.168.**.**. It would be interesting to edit the answer

  • 4

    I left my -1 too, as there has not yet been a position on the edition of the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.