ted_chou12 Posted February 5, 2007 Share Posted February 5, 2007 an ip looks roughly like this: 123.456.789.012 i want the forth combination to be censored to xxx, is it possible? Ted Link to comment https://forums.phpfreaks.com/topic/37174-ip-address-last-code-censored/ Share on other sites More sharing options...
effigy Posted February 5, 2007 Share Posted February 5, 2007 <pre> <?php // Run 10 tests. for ($i = 0; $i < 10; $i++) { // Generate an IP. for ($j = 0; $j < 4; $j++) { $ip_pieces[] = rand(1, 255); } $ip = join($ip_pieces, '.'); // Run regex. printf('%15s => %-15s<br>', $ip, preg_replace('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d+/', '\1xxx', $ip)); // Reset. $ip_pieces = null; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/37174-ip-address-last-code-censored/#findComment-177592 Share on other sites More sharing options...
mjlogan Posted February 6, 2007 Share Posted February 6, 2007 a messy looking one-liner, using no expressions, kind of not the point of this forum substr(implode("", array(substr($myIP, 0, strrpos($myIP, ".")+1), 'xxx')), 0, strlen($myIP)); Link to comment https://forums.phpfreaks.com/topic/37174-ip-address-last-code-censored/#findComment-178315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.