Re: regex and global vars problem
| From: | Jason Gerfen | Date: | Wed, 26 Oct 2005 18:07:54 +0000 |
| Subject: | Re: regex and global vars problem | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-224690@lists.php.net to get a copy of this message | ||
Um I did actually, but I will re-interate the problem with more detail.
the vars $mac1, $mac2, & $mac3 are to get passed to the chk_mac() function which determines if it is a valid hex representation of a h/w address, if it does not meet the criteria of having a ":" separating every two characters it then passes the var to the fix_mac() function which attempts to fix the string or h/w address by either replacing any "-" with a ":" or to break the h/w address up and insert a ":" every two characters. I also believe this is the one you should be playing with as the last post I added a new regex which wasn't working.
Any help is appreciated.
<?php
/*
* function to check validity of MAC address for the ISC DHCPD daemon
* 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;
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>";
}
?>
Jasper Bryant-Greene wrote:
On Wed, 2005-10-26 at 11:15 -0600, Jason Gerfen wrote:-- Jason Gerfen "My girlfriend threated to leave me if I went boarding... I will miss her." ~ DIATRIBE aka FBITKKI 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.[snip] Would you mind telling us what the problem was?