PHP把获取的IP替换成*

来源:百度知道 编辑:UC知道 时间:2024/06/02 18:53:54
例如IP:125.166.231.11
用PHP把这个IP中的166.231替换成***.***后
显示的IP为:125.***.***.11

<?php
$str = '125.166.231.11';
echo preg_replace('/^(\d+)\.(?:\d+)\.(?:\d+)\.(\d+)$/' , '$1.***.***.$2' , $str);
?>

$ip = "123.123.123.12";
$arr = explode(".",$ip);
$arr[1] = $arr[2] = "*";
$ip = implode(".", $arr);

$str = "125.166.231.11";
$str = str_replace("166.231","***.***",$str);
echo $str;

支持 andyfoo 数组的方式最简便;

el13 的 - -! 诶.....