Home Backend Development PHP Tutorial How to verify if input is an IPv4 address using regex in PHP

How to verify if input is an IPv4 address using regex in PHP

Jun 24, 2023 am 09:20 AM
php regular expression Input validation IPv address verification

PHP, as a popular server-side programming language, provides some powerful tools to verify the correctness of input data. In this article, we will focus on how to use regular expressions to verify whether the input is an IPv4 address.

First of all, what is an IPv4 address? An IPv4 address refers to a 32-bit binary number, which is usually divided into four 8-bit binary numbers, separated by ".", and expressed in decimal form. For example, 127.0.0.1 is an IPv4 address.

Now, let’s see how to use regular expressions to verify whether the input is an IPv4 address. The following is a basic regular expression example:

/^(?:[0-9]{1,3}.){3}[0-9]{1,3}$/
Copy after login

This regular expression consists of three parts. The first part is ^, which is used to specify the starting position of the match. The second part is (?:[0-9]{1,3}.){3}, which means matching a combination of three numbers and a dot repeated three times. This combination can match the first three numbers. and dots. (?:) indicates a non-capturing group, that is, it will not be recorded when matching. The last part is [0-9]{1,3}$, which means matching the last number, no other characters after it, and ending with this number.

However, there are still some problems with this regular expression. It allows matching some impossible IPv4 addresses, such as the broadcast address of 256.0.0.0 or 192.168.1.0/24.

In order to solve this problem, we need to add some logic to verify whether each number is between 0~255. The following is an example of a regular expression for verifying an IPv4 address:

/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Copy after login

This regular expression also includes three parts. The first part is (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)., which means matching A number starting with 25, starting with 24 and the second number is between 0 and 9, or starting with 0 and 9, in all three cases followed by a dot. This combination will match the first three numbers and the dot.

The second part is the first part repeated three times. The last part is (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?), which means matching Numbers starting with 25, starting with 24 and the second number is between 0 and 9, or starting with 0 and 9. This combination will match the last number.

This regular expression can verify the validity of an IPv4 address, capture each number accurately, and ensure that each number is between 0 and 255.

Here is a sample code that demonstrates how to use this regular expression to check an IP address:

function isIPv4($ip) {
  // 强制类型转换确保$ip为字符串类型
  $ip = (string)$ip;
  
  // 匹配IPv4地址
  $pattern = '/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
  
  return preg_match($pattern, $ip);
}

// 测试验证
var_dump(isIPv4('127.0.0.1')); // true
var_dump(isIPv4('256.0.0.0')); // false
Copy after login

In the above example, we defined a isIPv4() Function to verify an IPv4 address. We cast the IP address to a string type and match it against a regular expression, returning true if the match is successful, otherwise false.

In practical applications, it is very important to verify the data entered by the user. Using regular expressions to validate IPv4 addresses is a good choice for a quick and easy way to check the correctness of user input.

The above is the detailed content of How to verify if input is an IPv4 address using regex in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to verify if input is an IPv6 address using PHP regex How to verify if input is an IPv6 address using PHP regex Jun 25, 2023 am 09:37 AM

IPv6 refers to InternetProtocolVersion6, which is an IP address protocol used for Internet communication. An IPv6 address is a number composed of 128 bits, usually represented by eight hexadecimal number groups. In PHP, you can use regular expressions to verify whether the input is an IPv6 address. Here's how to use PHP regular expressions to verify IPv6 addresses. Step 1: Understand the format of the IPv6 address. The IPv6 address consists of 8 hexadecimal blocks, each

How to verify if string is empty with PHP regular expression How to verify if string is empty with PHP regular expression Jun 24, 2023 am 08:46 AM

In PHP, we can use regular expressions to verify whether a string is empty. Cases where the string is empty include the following: The string contains only spaces. The string length is 0. String is null or undefined. Next, we'll cover how to use regular expressions in PHP to validate these situations. Regular expression: s+ This regular expression can be used to match strings containing only spaces. Where s means matching spaces, + means matching one or more. Code example: functionisEmptySt

How to validate phone number format with PHP regular expression How to validate phone number format with PHP regular expression Jun 24, 2023 am 08:44 AM

When writing web applications, you often need to verify phone numbers. A common method in PHP is to use regular expressions to determine whether the phone number is in the correct format. Regular expressions are a powerful tool that can help you identify certain patterns in concise statements. Below is an example of using regular expressions in PHP to validate phone number format. First, let's define the common format for phone numbers. Phone numbers can contain numbers, parentheses, hyphens, and spaces. A standard phone number should contain 10 digits, preceded by

How to verify URL address format with PHP regular expression How to verify URL address format with PHP regular expression Jun 24, 2023 am 09:51 AM

With the rapid development of the Internet, URL addresses have become an indispensable part of people's daily lives. In web development, in order to ensure that the URL address entered by the user can be correctly recognized and used by the system, we need to perform format verification on it. This article will introduce how to use PHP regular expressions to verify URL address format. 1. Basic components of URL addresses Before understanding how to verify the URL address format, we first need to understand the basic components of URL addresses. Usually, a standard URL address consists of

How to verify if it is a file path using regular expression in PHP How to verify if it is a file path using regular expression in PHP Jun 24, 2023 am 10:18 AM

In PHP, regular expressions are a commonly used string matching and validation tool. During the development process, the input file path needs to be frequently verified to ensure that it is in the correct format. This article will introduce how to use regular expressions to verify whether a string is a file path. First, we need to determine the basic format of a file path. In Windows systems, a typical file path is in a format similar to "C:ProgramFilesPHPphp.exe". The path is divided into the following parts:

PHP regular expression to verify whether the input string is in the format of ID number or passport number PHP regular expression to verify whether the input string is in the format of ID number or passport number Jun 24, 2023 pm 12:11 PM

ID number and passport number are common document numbers in people's lives. When implementing functions involving these document numbers, it is often necessary to perform format verification on the entered numbers to ensure their correctness. In PHP, regular expressions can be used to achieve this function. This article will introduce how to use PHP regular expressions to verify whether the input string is in the format of an ID number or passport number. 1. ID card number verification The ID card number is composed of 18 digits and the last digit may be a letter (check code). Its format is as follows: the first 6

How to avoid XSS attacks in PHP language development? How to avoid XSS attacks in PHP language development? Jun 10, 2023 pm 04:18 PM

With the popularity of the Internet, website security issues have received more and more attention. Among them, XSS attacks are one of the most common and dangerous security threats. The full name of XSS is Cross-sitescripting, which is translated in Chinese as cross-site scripting attack. It means that the attacker deliberately inserts a piece of malicious script code into the web page, thus affecting other users. PHP language is a language widely used in web development, so how to avoid XSS attacks in PHP language development? This article will elaborate on the following aspects. 1. Parameterized query

How to verify IPv4 address using regular expression in golang How to verify IPv4 address using regular expression in golang Jun 24, 2023 am 08:31 AM

In the field of computer network and system management, IP address is a very important concept. Among them, the IPv4 address is the most common one. It consists of four decimal numbers (each number is between 0 and 255), connected by a dot (.) in the middle. In Golang, regular expressions can be easily used to verify the validity of IPv4 addresses. Here's how to use regular expressions to verify IPv4 addresses: First, we need to use regular expressions to match IPv4 addresses. IPv4 address consists of 4 numbers, each number starts from 0

See all articles