php-regexHow to use PHP regex to match an IP address?
To match an IP address using PHP regex, you can use the following code:
$ip = '127.0.0.1';
if (preg_match('/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip)) {
echo 'IP address is valid';
} else {
echo 'IP address is not valid';
}
Output example
IP address is valid
The code consists of the following parts:
-
$ip = '127.0.0.1';
- This is the IP address that we are trying to match. -
preg_match('/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip)
- This is the regular expression used to match the IP address. It consists of two parts:^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}
- This part matches the first three octets of the IP address. It consists of three parts:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
- This part matches a single octet of the IP address. It consists of five parts:[0-9]
- This part matches any number between 0 and 9.[1-9][0-9]
- This part matches any number between 10 and 99.1[0-9]{2}
- This part matches any number between 100 and 199.2[0-4][0-9]
- This part matches any number between 200 and 249.25[0-5]
- This part matches any number between 250 and 255.
\
- This is an escape character.\.
- This part matches a single dot.
([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
- This part matches the fourth octet of the IP address. It is the same as the first part.
-
if (preg_match('/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip)) {
- This is an if statement that checks if the IP address matches the regular expression. -
echo 'IP address is valid';
- This is the code that is executed if the IP address matches the regular expression. -
echo 'IP address is not valid';
- This is the code that is executed if the IP address does not match the regular expression.
Helpful links
More of Php Regex
- How to match a space using PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to get the first match when using regex in PHP?
- How to match the end of a string when using regex in PHP?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match special characters?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match UUID?
- How to match strings starting with a certain string using PHP regex?
See more codes...