I am having a problem with a couple of function I have written to check for a type of string, attempt to fix it and pass it back to the main function. Any help is appreciated.

<?php

/*
* ex. 00:AA:11:BB:22:CC
*/
function chk_mac( $mac ) {
if( ( eregi( "^[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}$", $mac ) ) || ( !eregi( "^[0-9a-fA-F]$", $mac ) ) {
 return 0;
} else {
 return 1;
}
}

/*
* check validity of MAC & do replacements if necessary
*/
function fix_mac( $mac ) {
global $mac;

if( eregi( "^[0-9A-Fa-f-\:]$", $mac ) ) {
    $mac1 = $mac;
    echo "MAC: $mac1<br>";
   }

   /* strip the dash & replace with a colon */
if( eregi( "^[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}$", $mac ) ) {
 $mac1 = preg_replace( "/\-/", ":", $mac );
 echo "MAC: $mac1<br>";
   }
/* add a colon for every two characters */
if( eregi( "^[0-9A-Fa-f]{12}$", $mac ) ) {
 /* split up the MAC and assign new var names */
@list( $mac_1, $mac_2, $mac_3, $mac_4, $mac_5, $mac_6 ) = @str_split( $mac, 2 );
 /* put it back together with the required colons */
$mac1 = $mac_1 . ":" . $mac_2 . ":" . $mac_3 . ":" . $mac_4 . ":" . $mac_5 . ":" . $mac_6;
 echo "MAC: $mac1<br>";
}
return $mac1;
}

// do our checks to make sure we are using these damn things right
$mac1 = "00aa11bb22cc";
$mac2 = "00-aa-11-bb-22-cc";
$mac3 = "00:aa:11:bb:22:cc";
$mac4 = "zz:00:11:22:ff:xx";

if( chk_mac( $mac1 ) != 0 ) {
$mac = fix_mac( $mac1 );
   echo $mac1 . " converted to " . $mac . "<br>";
} else {
echo "$mac1 is valid.<br>";
}

if( chk_mac( $mac2 ) != 0 ) {
$mac = fix_mac( $mac2 );
   echo $mac2 . " converted to " . $mac . "<br>";
} else {
echo "$mac2 is valid.<br>";
}

if( chk_mac( $mac3 ) != 0 ) {
$mac = fix_mac( $mac3 );
   echo $mac3 . " converted to " . $mac . "<br>";
} else {
echo "$mac3 is valid.<br>";
}

if( chk_mac( $mac4 ) != 0 ) {
$mac = fix_mac( $mac4 );
   echo $mac4 . " converted to " . $mac . "<br>";
} else {
echo "$mac4 is valid.<br>";
}


?>

--
Jason Gerfen

"My girlfriend threated to
leave me if I went boarding...
I will miss her."
~ DIATRIBE aka FBITKK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to