You are currently viewing PHP: How to Validate MAC Addresses with filter_var() Function

PHP: How to Validate MAC Addresses with filter_var() Function

Validating MAC (Media Access Control) addresses is a crucial task in network programming and security. Ensuring that the entered MAC addresses are in the correct format helps maintain data integrity and prevents potential vulnerabilities. In PHP, the filter_var() function offers a convenient and efficient way to validate MAC addresses.

The code validates whether the given MAC address is in a valid format or not using the filter_var() function with the FILTER_VALIDATE_MAC filter.

<?php

$mac_address = '00:1B:44:11:3A:B7';

if (filter_var($mac_address, FILTER_VALIDATE_MAC)) {

    echo "The MAC address '$mac_address' is valid.";

} else {

    echo "The MAC address '$mac_address' is not valid.";

}

Note that this code requires PHP version 5.5.0 or higher, as the FILTER_VALIDATE_MAC filter was introduced in PHP 5.5.0. If you wish to learn more about PHP, please subscribe to our newsletter today and continue your PHP learning journey with us!

Leave a Reply