How can I simply validate whether a string is a valid IP in PHP?

后端 未结 6 1947
再見小時候
再見小時候 2021-01-04 03:01

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

Examples of valid strings:

  • 192.158.5.95
  • 121.212
  • 12.12.12.204
6条回答
  •  [愿得一人]
    2021-01-04 03:47

    iNaD ist right, the filter_var() version is the best one - ip2long() is really not that failsave. Just try to put in some strange things and see for yourself. I did a performace-check and filter_var is pretty much faster than the common regex versions.

提交回复
热议问题