Pejman Moghadam / Scripts

PHP Script - Validate and normalize MAC address

Public domain


<?php

function is_valid_mac($mac)
{
  // 01:23:45:67:89:ab
  if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', $mac))
    return true;
  // 01-23-45-67-89-ab
  if (preg_match('/^([a-fA-F0-9]{2}\-){5}[a-fA-F0-9]{2}$/', $mac))
    return true;
  // 0123456789ab
  else if (preg_match('/^[a-fA-F0-9]{12}$/', $mac))
    return true;
  // 0123.4567.89ab
  else if (preg_match('/^([a-fA-F0-9]{4}\.){2}[a-fA-F0-9]{4}$/', $mac))
    return true;
  else
    return false;
}

function normalize_mac($mac)
{
  // remove any dots
  $mac =  str_replace(".", "", $mac);

  // replace dashes with colons
  $mac =  str_replace("-", ":", $mac);

  // counting colons
  $colon_count = substr_count ($mac , ":");

  // insert enough colons if none exist
  if ($colon_count == 0)
  {
    $mac =  substr_replace($mac, ":", 2, 0);
    $mac =  substr_replace($mac, ":", 5, 0);
    $mac =  substr_replace($mac, ":", 8, 0);
    $mac =  substr_replace($mac, ":", 11, 0);
    $mac =  substr_replace($mac, ":", 14, 0);
  }

  // uppercase
  $mac = strtoupper($mac);

  // DE:AD:BE:EF:10:24
  return $mac;
}

function check($mac)
{
  echo $mac." => ";
  if(is_valid_mac($mac))
    echo normalize_mac($mac);
  else
    echo "Invalid MAC";
  echo "\n<br>";
}

$mac="de:ad:be:ef:10:24";
check($mac);
$mac="de-ad-be-ef-10-24";
check($mac);
$mac="deadbeef1024";
check($mac);
$mac="dead.beef.1024";
check($mac);

?>

BY: Pejman Moghadam
TAG: php
DATE: 2012-06-26 17:10:29


Pejman Moghadam / Scripts [ TXT ]