note 45686 deleted from function.preg-replace by aidan
| From: | aidan@php.net | Date: | Wed, 15 Sep 2004 11:25:27 +0000 |
| Subject: | note 45686 deleted from function.preg-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76730@lists.php.net to get a copy of this message | ||
Note Submitter: anonymous at ayexample dot com
----
Just finished a nice short an sweet function to convert MAC addresses into several different
formats:
<?php
// Generic MAC Address conversion routine
function cvtmac($macaddr, $format=":", $width=2, $case=0) {
// Strip all but 0-F ignoring case
$macaddr = preg_replace("/[^0-9A-F]*/i", "", $macaddr);
if (strlen($macaddr) != 12) {
return "";
}
if ($case == 0) {
$macaddr = strtolower($macaddr);
} else {
$macaddr = strtoupper($macaddr);
}
return substr(chunk_split($macaddr, $width, $format), 0, -1);
}
?>
Example uses:
<?php echo cvtmac("0000 C021 D580"); ?>
RESULT: 00:00:c0:21:d5:80
<?php echo cvtmac("0000 C021 D580", ':', 2, 1); ?>
RESULT: 00:00:C0:21:D5:80
<?php echo cvtmac("0000 C021 D580", '.', 4, 0); ?>
RESULT: 0000.c021.d580