Skip to content

Instantly share code, notes, and snippets.

@hlev
Last active May 28, 2019 23:10
Show Gist options
  • Save hlev/eab73418ab20cdca3ff9 to your computer and use it in GitHub Desktop.
Save hlev/eab73418ab20cdca3ff9 to your computer and use it in GitHub Desktop.
PHP regexp testing for valid MAC address
<?php
/**
* To match aa:bb:cc:dd:ee:ff, aabbcddeeff, aa-bb-cc-dd-ee-ff formats in case-insensitive manner
*/
$macAddress = '2a:3b:2c:1d:44:23';
// 1
echo preg_match('/^(?:(?:[0-9a-f]{2}[\:]{1}){5}|(?:[0-9a-f]{2}[-]{1}){5}|(?:[0-9a-f]{2}){5})[0-9a-f]{2}$/i', $macAddress);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment