regular expression help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Simple functions to check & fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() 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 ) ) {
return 0;
} else {
return 1;
}
}


/*
* check validity of MAC & do replacements if necessary
*/
function fix_mac( $mac ) {
global $mac;
/* 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 ) ) {
$mac = preg_replace( "/\-/", ":", $mac );
return $mac;
}
/* 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( $mac1, $mac2, $mac3, $mac4, $mac5, $mac6 ) = @str_split( $mac, 2 );
/* put it back together with the required colons */
$mac = $mac1 . ":" . $mac2 . ":" . $mac3 . ":" . $mac4 . ":" . $mac5 . ":" . $mac6;
return $mac;
}
}


// 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";

// make sure it is global
global $mac;

// if mac submitted is invalid check & fix if necessary
if( chk_mac( $mac1 ) != 0 ) {
 $mac = fix_mac( $mac1 ); echo $mac1 . " converted to " . $mac . "<br>";
}
if( chk_mac( $mac2 ) != 0 ) {
 $mac = fix_mac( $mac2 ); echo $mac2 . " converted to " . $mac . "<br>";
}
if( chk_mac( $mac3 ) != 0 ) {
 $mac = fix_mac( $mac3 ); echo $mac3 . " converted to " . $mac . "<br>";
}

?>
--
Jason Gerfen

"And remember... If the ladies
 don't find you handsome, they
 should at least find you handy..."
             ~The Red Green show

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux