PHP中的部分字符串比较
我已经在两个字符串中存储了两个 IP 地址值,
a = '116.95.123.111'
b = '116.95.122.112'
我只想比较两个字符串中 IP 地址的前两个部分,即 116.95
部分,因为这两个字符串中的值相同,所以我的比较应该返回 true 。如何在 PHP 中进行部分字符串比较?
i have stored two ip address values in two strings
a = '116.95.123.111'
b = '116.95.122.112'
i just want to compare the first two parts of ip address i.e 116.95
part in the two strings and as it is same in both the strings my comparison should return true. how to do this partial string comparison in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
substr_compare
是二进制安全的。substr_compare
is binary safe.IP 地址可以包含前导零。爆炸时,可以将它们作为整数进行比较,以便忽略前导零。
“干净”的方式是这样的......
IP adresses can contain leading zeroes. When exploding, they can be compared to as integers, so that leading zeroes are ignored.
The "clean" way would be by something like this...
在这种特殊情况下(比较 IP 地址)
in this particular case (comparing ip addresses)