Guest User

Untitled

a guest
Jan 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Filters out valid mac characters to return a uniform MAC format
  5. * @param String $mac The raw string expression
  6. * @return String The filtered/formatted MAC address
  7. */
  8. function format_mac_address($mac)
  9. {
  10. if(!$mac || $mac == "")
  11. {
  12. // If no MAC was passed, return;
  13. return null;
  14. }
  15. preg_match_all('/[0-9a-fA-F]/', $mac, $matches);
  16. if(!$matches || sizeof($matches[0]) != 12)
  17. {
  18. // If the number of filtered characters is not 12, return for invalid mac;
  19. return null;
  20. }
  21. $filteredMac = "";
  22. foreach($matches[0] as $match)
  23. {
  24. // Add each character into a string and capitalize the alphas
  25. $filteredMac .= strtoupper($match[0]);
  26. }
  27. // Split the characters into segments of 2
  28. $macSegmenets = str_split($filteredMac,2);
  29. // Glue the segments together with a colon
  30. $finalMac = implode(":",$macSegmenets);
  31. return $finalMac;
  32. }
Add Comment
Please, Sign In to add comment