note 45686 added to function.preg-replace
| From: | anonymous at ayexample dot com | Date: | Wed, 15 Sep 2004 04:29:22 +0000 |
| Subject: | note 45686 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76707@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45686
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45686&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45686&report=yes
Search -- http://master.php.net/manage/user-notes.php