Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Filters out valid mac characters to return a uniform MAC format
- * @param String $mac The raw string expression
- * @return String The filtered/formatted MAC address
- */
- function format_mac_address($mac)
- {
- if(!$mac || $mac == "")
- {
- // If no MAC was passed, return;
- return null;
- }
- preg_match_all('/[0-9a-fA-F]/', $mac, $matches);
- if(!$matches || sizeof($matches[0]) != 12)
- {
- // If the number of filtered characters is not 12, return for invalid mac;
- return null;
- }
- $filteredMac = "";
- foreach($matches[0] as $match)
- {
- // Add each character into a string and capitalize the alphas
- $filteredMac .= strtoupper($match[0]);
- }
- // Split the characters into segments of 2
- $macSegmenets = str_split($filteredMac,2);
- // Glue the segments together with a colon
- $finalMac = implode(":",$macSegmenets);
- return $finalMac;
- }
Add Comment
Please, Sign In to add comment