Core\Validate::isMac
Core\Validate::isMac(string $s): boolCore\Validate::isMac( string $s, ): bool
Checks whether a text is a MAC address. A MAC address is the hardware address of a network card. The method returns true or false.
There are three ways to write a MAC address. The first is six pairs of hex digits joined by :, such as 00:1a:2b:3c:4d:5e. The second is the same six pairs joined by -. The third is three groups of four hex digits joined by ., such as 001a.2b3c.4d5e. Upper case and lower case letters are both allowed.
One text uses one separator. A text that mixes : and - gives false. A group with one digit, a missing group, a space or twelve digits with no separator also give false.
The examples below check a few texts, show the three ways to write the same address, and check the addresses in a list of devices.
Parameters
$sstringThe text to test.
Return value
Returns bool. true for one of the three ways to write it, in upper or lower case. false otherwise. A text that mixes two separators, such as 00:1a-2b:3c:4d:5e, gives false.
Examples
Check a few texts
<?nvs// Shows which texts are MAC addresses.
// "00:1a-2b:3c:4d:5e" mixes ":" and "-", and "00:1a:2b:3c:4d" has only five pairs.// "001a2b3c4d5e" has no separator, and "00:1g:2b:3c:4d:5e" contains "g".array<string> $texts = ['00:1a:2b:3c:4d:5e', '00:1a-2b:3c:4d:5e', '00:1a:2b:3c:4d', '001a2b3c4d5e', '00:1g:2b:3c:4d:5e'];
// `Core\Validate::isMac` returns `true` for a MAC address.// This prints yes for the first text and no for the other four.foreach ($texts as string $text) { echo $text, ': ', Core\Validate::isMac($text) ? 'yes' : 'no', "\n";}00:1a:2b:3c:4d:5e: yes00:1a-2b:3c:4d:5e: no00:1a:2b:3c:4d: no001a2b3c4d5e: no00:1g:2b:3c:4d:5e: noThree ways to write one address
<?nvs// Shows the three ways to write the same MAC address.
// The same address with ":", with "-" and with ".".// The last one is in upper case. Upper and lower case letters are both allowed.array<string> $forms = ['00:1a:2b:3c:4d:5e', '00-1a-2b-3c-4d-5e', '001A.2B3C.4D5E'];
// This prints yes three times.foreach ($forms as string $form) { echo $form, ': ', Core\Validate::isMac($form) ? 'yes' : 'no', "\n";}00:1a:2b:3c:4d:5e: yes00-1a-2b-3c-4d-5e: yes001A.2B3C.4D5E: yesCheck a list of devices
<?nvs// Shows how to check the MAC addresses in a list of devices before a program saves them.
// Each device name has the address somebody typed in for it.array<string> $devices = [ 'printer' => '00:1A:2B:3C:4D:5E', 'camera' => ' 00-1a-2b-3c-4d-5f ', 'router' => '00:1a:2b:3c:4d',];
// `trim` removes the spaces, and `lower` makes the letters lower case.// The printer and the camera are saved. The router address has only five pairs.foreach ($devices as string $device => string $typed) { string $mac = Core\Str::lower(Core\Str::trim($typed)); if (Core\Validate::isMac($mac)) { echo $device, ': saved ', $mac, "\n"; } else { echo $device, ": not a MAC address\n"; }}printer: saved 00:1a:2b:3c:4d:5ecamera: saved 00-1a-2b-3c-4d-5frouter: not a MAC address