-1

I have a scenario that i have to replace after the last point must be replaced with asterisks for the IP address.

Wrong example: 123.456.789
Correct example: 123.456.***

How is this possible?

Noobie
  • 99
  • 1
  • 11
  • 2
    What did you try? – anubhava Dec 10 '19 at 11:31
  • `strlen`, `str_repeat`, `strrpos` already invented – u_mulder Dec 10 '19 at 11:34
  • try https://stackoverflow.com/questions/17030779/splitting-strings-in-php-and-get-last-part – Tharindu Dec 10 '19 at 11:35
  • echo preg_replace('/(?!\d{1,3}\.\d{1,3}\.)\d/', '*', "123.456.789"); – BhAvik Gajjar Dec 10 '19 at 11:36
  • There is tons of options how you could to by regex. [See this demo for the first 5 I could think of](https://tio.run/##jdHLSsQwFAbgfZ/iWArTlCGlc/GCDt05SjcFt0GJzbENU5KQpoIw714TGXQWVVwmHL4//4npzDTdlfVDHUXJ4CzsIC5Wa7rZXtKr65v4NoryHApKYBykaoEr0MZJrXgPDTdutAit1aOJsOk0GIvti0XT8wbTRZ7SjFFSMpEvlhAnRRYvIaQQoBAzddJXPzoYPUgn3xF6rQ@8Qy5mYSbScsdElpAved5dn7kKW/5P98K7/tV/wBsPDwdpTLANtw70G7gOwU@Gq9cPSLOn6rEmaXZPZoPCYs5mjqcVzedtv4uwCpwGiwM6/xUC2D6cG62cVCPOV9r7Ss@EiWPIZNXvSdP0CQ) and please always show what you tried and include in question! – bobble bubble Dec 10 '19 at 11:55

2 Answers2

0
$ip  = "XXX.XXX.XXX";
$parts = explode(".", $ip);
if(count($parts) >= 2){
    $result = $parts[0].".".$parts[1]."."."***";
}
echo $result;
Minirock
  • 628
  • 3
  • 13
  • 28
0

explode() can be used

$a = '123.456.789';
$b = explode('.', $a);
print_r(str_replace(end($b), '***', $b));
Swetha Sekar
  • 237
  • 1
  • 9