You can use this package to easily anonymize IP addresses in your Laravel application. Requires PHP 7.1 or higher.
You can install the package via composer:
composer require cschalenborgh/laravel-ip-anonymizer
The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
'providers' => [
// ...
Cschalenborgh\IpAnonymizer\IpAnonymizerServiceProvider::class,
];
echo IpAnonymizer::anonymizeIp('133.242.241.12');
// returns 133.242.241.0
echo IpAnonymizer::anonymizeIp('133.242.241.12', '255.255.0.0');
// returns 133.242.0.0
echo IpAnonymizer::anonymizeIp('2001:db8:85a3::1319:8a2e:370:7344', 'ipv6');
// returns 2001:db8:85a3::
echo IpAnonymizer::anonymizeIp('2001:db8:85a3::1319:8a2e:370:7344', 'ipv6', 'ffff:ffff:0000:0000:0000:0000:0000:0000');
// returns 2001:db8::
echo (new IpAnonymizer())->anonymizeIPv4('133.242.241.12');
// returns 133.242.241.0
echo (new IpAnonymizer())->anonymizeIPv4('133.242.241.12', '255.0.0.0');
// returns 133.0.0.0
echo (new IpAnonymizer())->anonymizeIPv6('2001:db8:85a3::1319:8a2e:370:7344');
// returns 2001:db8:85a3::
echo (new IpAnonymizer())->anonymizeIPv6('2001:db8:85a3::1319:8a2e:370:7344', 'ffff:ffff:0000:0000:0000:0000:0000:0000');
// returns 2001:db8::