Re: regex and global vars problem

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

 



On Wed, 2005-10-26 at 12:24 -0600, Jason Gerfen wrote:
> The code I just showed you is supposed to do the following, the 
> chk_mac() returns a true or false on the vars $mac1, $mac2 and $mac3.  
> $mac3 is the only var that should not be thrown into the fix_mac() 
> function which is working correctly.  The problem is when $mac1 and 
> $mac2 get put into the fix_mac() function nothing is being returned.
> 
> I am not recieving any error codes, just an empty var.
> 
> Sample output:
> 
> 00aa11bb22cc converted to
> 00-aa-11-bb-22-cc converted to
> 00:aa:11:bb:22:cc is valid.
> 
> As you can see $mac3 is a valid example of a h/w address where $mac1 & 
> $mac2 were returned from the fix_mac() function as an empty string.

OK, thanks for that. I've rewritten your function below in much cleaner
code. Some of the situations you've used regexps in, string functions
would work fine. They're also much faster.

The below function works fine in my testing. I removed the special-case
for a valid MAC address at the start since your regexp was throwing
errors and there was no need since you already checked if it was valid
with chk_mac() (which by the way should return true or false, not 1 or
0, but meh).

<?php
function fix_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
	) ) {

		$mac_final = str_replace( '-', ':', $mac );

	} else if( eregi( "^[0-9A-Fa-f]{12}$", $mac ) ) {

		$mac_array = str_split( $mac, 2 );
		$mac_final = implode( ':', $mac_array );

	} else {

		return false;

	}

	echo "MAC: $mac_final";
	return $mac_final;

}
?>

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: jasper@xxxxxxxxxxx
w: http://www.album.co.nz/
p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303
a: PO Box 579, Christchurch 8015, New Zealand

-- 
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