Net_MAC::format()

Net_MAC::format() – Cleanly formats Media Access Control (MAC) addresses

Synopsis

require_once 'Net/MAC.php';

string Net_MAC::format ( string $input , string $delimiter=':' , boolean $uppercase = true )

Description

This function will format a MAC address into XX:XX:XX:XX:XX:XX format from whatever format is passed to the function. The delimiter (':' in the example above) will be replaced with whatever string is passed to the $delimiter parameter (default ':').

Parameter

  • string $input - The string containing the MAC Address

  • string $delimiter - The string representing the delimiter to use when formatting the MAC Address

  • string $uppercase - If set to TRUE (default), the alpha characters in the hexadecimal values in the MAC Address will be returned in uppercase. If FALSE, the alpha characters in the hexadecimal values will be returned in lowercase.

Return value

string - The formatted MAC Address or FALSE if the syntax of the MAC address is invalid

Note

This function should be called statically.

Example

Using format()

<?php
require_once "Net/MAC.php";

$macaddr 'AB:CD:EF:00:11:22';

if (!
Net_MAC::check($macaddr)) {
    echo 
"$macaddr is invalid";
    exit;
}

$mac Net_MAC::format($macaddr);
if (
$mac) {
    echo 
"$mac";
}
else {
    echo 
"$macaddr could not be formatted";
}
?>

This would output the following:

AB:CD:EF:00:11:22

Using format() to get a MAC address with a different delimiter

<?php
require_once "Net/MAC.php";

$macaddr 'AB:CD:EF:00:11:22';

if (!
Net_MAC::check($macaddr)) {
    echo 
"$macaddr is invalid";
    exit;
}

$mac Net_MAC::format($macaddr'-');
if (
$mac) {
    echo 
"$mac";
}
else {
    echo 
"$macaddr could not be formatted";
}
?>

This would output the following:

AB-CD-EF-00-11-22

Using format() to get a MAC address with all capital alpha characters

<?php
require_once "Net/MAC.php";

$macaddr 'ab:cd:ef:00:11:22';

if (!
Net_MAC::check($macaddr)) {
    echo 
"$macaddr is invalid";
    exit;
}

$mac Net_MAC::format($macaddr''true);
if (
$mac) {
    echo 
"$mac";
}
else {
    echo 
"$macaddr could not be formatted";
}
?>

This would output the following:

ABCDEF001122