* @license BSD */ //namespace Domframework; /** Manage the MAC addresses * Based on : * https://www.stetsenko.net/2011/01/php-mac-address-validating-and-formatting/ * The MAC format can be : * - de:ad:be:af:aa:bb * - de-ad-be-af-aa-bb * - 0005.313B.9080 * - 0005313B9080 */ class macaddresses { /** This method return true if the provided MAC address is valid * return false if the mac is not valid * The MAC format can be : * - de:ad:be:af:aa:bb * - de-ad-be-af-aa-bb * - 0005.313B.9080 * - 0005313B9080 * @param string $mac The mac to check * @return bool */ public static function isMACAddress ($mac) // {{{ { if (! is_string ($mac)) return false; return ( preg_match ("/^([a-fA-F0-9]{2}[-:]){5}[0-9A-Fa-f]{2}$/", $mac) === 1 || preg_match ("/^([a-fA-F0-9]{4}[.]){2}[a-fA-F0-9]{4}$/", $mac) === 1 || preg_match ("/^[a-fA-F0-9]{12}$/", $mac) === 1); } // }}} /** Reform the mac address with the separator. * The provided mac MUST be without separator (can be removed by * removeSeparator method) * @param string $mac The mac address to update, without separator * @param string|null $separator The separator to use (: by default) * @param integer|null $nbdigit The number of digit to use * @return string The updated mac address with separators */ public static function addSeparator ($mac, $separator = ':', $nbdigit = 2) // {{{ { if (strspn ($mac, "0132465789ABCDEFabcdef") !== strlen ($mac)) throw new \Exception (dgettext ("domframework", "Invalid mac address provided to addSeparator : bad chars"), 406); return join ($separator, str_split ($mac, $nbdigit)); } // }}} /** Remove all the separators from the provided MAC address * @param string $mac the mac address to update * @param array|null $separators The separators to remove * @return the updated mac address without separators */ public static function removeSeparator ($mac, $separators = array (":", "-", ".")) // {{{ { return str_replace ($separators, '', $mac); } // }}} }