如何简单地验证字符串在PHP中是否为有效IP? [英] How can I simply validate whether a string is a valid IP in PHP?

查看:288
本文介绍了如何简单地验证字符串在PHP中是否为有效IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,如何验证字符串是有效IP?

Using PHP, how do I validate that a string is a valid IP?

有效字符串的示例:

  • 192.158.5.95
  • 121.212
  • 12.12.12.204

无效字符串的示例:

  • 121
  • 10 12 12(无点)

我当前的脚本使用此代码,但这不足以满足我的需求:

My current script uses this code, but this is insufficient for my needs:

if(strpos($input, '.') !== false)
{    
  // There is a period 
} 
else 
{     
  // No Period 
} 

这样,有人可以建议我如何验证字符串是有效的IP吗?

As such, can someone please advise how I can validate that a string is a valid IP?

推荐答案

$valid = ip2long($ip) !== false;

这篇关于如何简单地验证字符串在PHP中是否为有效IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆