tyra Posted May 23, 2009 Share Posted May 23, 2009 How to hide last digits of ip adress if i take them from arry? Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/ Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 preg_replace("/\.\d{1,3}$/",'xxx','123.230.192.222'); Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840463 Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 preg_replace("/\.\d{1,3}$/",'.xxx','123.230.192.222'); sorry.. use this instead Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840468 Share on other sites More sharing options...
tyra Posted May 23, 2009 Author Share Posted May 23, 2009 Thanks! But if i have that string longer than just ip then how can i find the end of it there? each ind of the ip is - Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840526 Share on other sites More sharing options...
Ken2k7 Posted May 23, 2009 Share Posted May 23, 2009 It should still work. What string do you have that the code is failing on or not producing the desired results/output? Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840528 Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 <?php echo preg_replace("/((?:[01]?\d\d?|2(?:[0-4]\d|5[0-5]))(?:\.(?:[01]?\d\d?|2(?:[0-4]\d|5[0-5]))){2})\.(?:[01]?\d\d?|2(?:[0-4]\d|5[0-5]))\b/",'$1.xxx','123.230.192.222'); ?> this works, just tested it Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840550 Share on other sites More sharing options...
tyra Posted May 23, 2009 Author Share Posted May 23, 2009 holy smokes! That surely isn't something to keep in mind. Can you explain a bit how did you came to this? Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840564 Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 (?:[01]?\d\d?|2(?:[0-4]\d|5[0-5])) this basically matches any number between 0 and 255, with the exception of like 001 which is also possible to see, akwardly enough, in an IP. then, I attempt to match that same thing, but with a dot at the front, 2 times, since you just want to remove the last number. (?:\.(?:[01]?\d\d?|2(?:[0-4]\d|5[0-5]))){2} and then, the last one, is just another match from 0 - 255, with a word boundary expected \b to tell it to attempt all numbers, instead of match then exit (?:[01]?\d\d?|2(?:[0-4]\d|5[0-5]))\b the first 3 parts, I captured it in a backreference, then used it in the replacement $1.xxx, which translates in plain english, {First 3 Parts}.xxx its regex, if you want to learn regex you should check out regexbuddy.com, they sell a product but they also give free regex tutorials. goodluck tyra, Russell Quote Link to comment https://forums.phpfreaks.com/topic/159345-solved-hide-last-digits-of-ip/#findComment-840581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.