Re: regular expression help

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

 



On Fri, 2005-01-21 at 10:12, 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 ) {
>   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 you want to use globals you need to use the global in the function
not the main body.

afaict you don't need globals at all you pass mac1 to each function and
use it as $mac in the function (exactly what you want to do) and return
local $mac to $mac in the main body.  perfectly legal.  If you set mac
to global in the function I suspect that you end up not using  the local
mac and are testing an empty var in the first pass of fix_mac.

remove the global statements and you are well on your way.

Bret

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

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