dongtan5811 2011-03-07 19:21
浏览 77
已采纳

如何使用PHP和正则表达式屏蔽/隐藏IP地址(字符串)

I would like to hide the last two sections from an IP address using regular expression the problem is that the asterix (*) must match the length of those sections.

Eg: 10.101.12.100 should be re-formated into 10.101.**.***

This is the code I'm working with :

echo preg_replace('!(\d+).(\d+).\d+.\d+!s', '${1}.${2}.***.***', "10.101.12.100");
// Return: 10.101.***.***

Is that possible using regex ?

PS: I know I could break it using explode('.', ...) along with str_repeat('*', strlen(...)) but I find preg_replace a cleaner solution. I'm looking for a "oneliner" solution.

  • 写回答

2条回答 默认 最新

  • dttwois6098 2011-03-07 19:29
    关注

    Use a negative look-ahead (Basically, have regex disqualify the first two octets, then do a normal digit replace from thereafter.) e.g.

    (?!\d{1,3}\.\d{1,3}\.)\d
    

    Demo

    Example output:

    237.134.85.92 -> 237.134.**.**
    173.14.176.182 -> 173.14.***.***
    167.209.41.203 -> 167.209.**.***
    137.133.204.130 -> 137.133.***.***
    93.108.72.157 -> 93.108.**.***
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码