00 Votes

PHP: Validate IP-Address

Question by Guest | 2015-05-29 at 21:03

Is there any PHP function with which you can check an IP address?

I am searching for a function which can tell me whether an arbitrary string is a valid IP address or not.

ReplyPositiveNegative
0Best Answer0 Votes

Since PHP version 5.2.0, you can use the function filter_var().

Here is a small example showing how to verify an IP address using filter_var().

$s = '192.168.0.0';

if (filter_var($s, FILTER_VALIDATE_IP)) {
    echo "$s is a valid ip address!";
} else {
    echo "$s is not a valid ip address!";
}

In this example, we are using filter_var() with the filter FILTER_VALIDATE_IP and we are checking the content of the variable $s.
2015-05-30 at 16:29

ReplyPositive Negative
Reply

Related Topics

How to avoid Spam Mails

Tip | 0 Comments

PHP: Validate E-Mail Address

Question | 2 Answers

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.