Reply to Re: [PHP] regex and global vars problem

Your name:

Reply:


Posted by Jasper Bryant-Greene on 10/26/05 21:47

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@album.co.nz
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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация