C3rd
[PHP] Converting a value to MAC address format
Posted: 20 Oct 2009, 23:37pm - Tuesday
I just want to post this since someone ask about it. It might help if someone in this world is searching for an answer with the same problem my friend does. This is about converting a value of 0014:f8c4:9e32 or 0014f8c49e32 to 00:14:f8:c4:9e:32...
Now here's some answers for that...
function convertMACAddress($input) { // clean up unwanted characters $input = preg_replace( '/[^a-zA-Z0-9]/', '', $input); // initiate character positioning $pos = 0; // declare output container $output = ''; // if given MAC is invalid, terminate process and return an error if (strlen($input) < 12) { return '[Unrecognized MAC address]'; } // initiate convertion... for ($i = 0; $i < 6; $i++) { if ($i == 0) { $output .= substr($input, $pos, 2); } else { $output .= ':'.substr($input, $pos, 2); } $pos += 2; } return $output; }That's it... Problem solved. :) If need a sample code, please download the source -> mac.convert.zip -- Have fun coding..