Current Location: Home> Latest Articles> How to Use the preg_match Function to Validate IP Addresses and Ensure Correct Format and Validity?

How to Use the preg_match Function to Validate IP Addresses and Ensure Correct Format and Validity?

gitbox 2025-08-27
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part is unrelated to the article content and can contain PHP comments or irrelevant code</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome to the PHP tutorial!"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"\n--------------------------------------------------\n"</span></span><span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p>How to Use the preg_match Function to Validate IP Addresses and Ensure Correct Format and Validity?</p>
<p>In PHP, the <code>preg_match

Explanation:

  • 25[0-5] matches 250–255

  • 2[0-4][0-9] matches 200–249

  • 1[0-9]{2} matches 100–199

  • class="fun">[1-9]?[0-9] matches 0–99

  • ((...)\.){3}... ensures four sections separated by dots

2. IPv6 Address Validation

IPv6 addresses are more complex, consisting of 8 groups of hexadecimal numbers separated by colons. A simple regex example is as follows:

<span><span><span class="hljs-variable">$ipv6_pattern</span></span><span> = </span><span><span class="hljs-string">"/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/"</span></span><span>;
</span><span><span class="hljs-variable">$ip</span></span><span> = </span><span><span class="hljs-string">"2001:0db8:85a3:0000:0000:8a2e:0370:7334"</span></span><span>;

</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">preg_match</span></span><span>(</span><span><span class="hljs-variable">$ipv6_pattern</span></span><span>, </span><span><span class="hljs-variable">$ip</span></span><span>)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"IPv6 address format is correct"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"IPv6 address format is incorrect"</span></span><span>;
}
</span></span>

3. Practical Tips

  1. If you want to validate both IPv4 and IPv6 at the same time, you can use the filter_var function, which has built-in IP validation:

<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">filter_var</span></span><span>(</span><span><span class="hljs-variable">$ip</span></span><span>, FILTER_VALIDATE_IP)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The IP address is valid"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The IP address is invalid"</span></span><span>;
}
</span></span>
  1. When handling user input, always remove spaces and invalid characters to avoid matching failures due to leading or trailing spaces:

<span><span><span class="hljs-variable">$ip</span></span><span> = </span><span><span class="hljs-title function_ invoke__">trim</span></span><span>(</span><span><span class="hljs-variable">$ip</span></span><span>);
</span></span>
  1. For batch validation of a large number of IP addresses, you can combine loops with preg_match to efficiently check each input.

Conclusion

By using preg_match along with precise regular expressions, you can validate whether an IP address format is correct and valid. For IPv4, the focus is on number ranges and segment count; for IPv6, the focus is on hexadecimal groups and colon separation. Mastering these techniques will make your PHP applications safer and more reliable when handling network addresses.

<span></span>