You are here: Re: regular expression help « PHP « IT news, forums, messages
Re: regular expression help

Posted by Jason on 01/21/05 18:52

You were right, I removed the reference to global $mac and it started
working great. Thanks. It seems sometimes another set of eyes to catch
stuff really helps... thanks.

Jason wrote:
> Jason wrote:
>
>> 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 ) {
>
> global $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>";
>> }
>>
>> ?>
>
> Still does not resolve the problem. declaring $mac as global in the
> chk_mac() function.
>


--
Jason Gerfen

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

 

Navigation:

[Reply to this 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

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