Re: [PHP] regex and global vars problem

2005-10-28 Thread Robin Vickery
On 10/28/05, Tom Rogers [EMAIL PROTECTED] wrote:

 I would do it with a small class like this:

 ?php
 class mac{
   var $mac='';
   var $is_valid = false;
   function mac($mac){
 $mac = preg_replace('/[^0-9A-F]/','',strtoupper($mac));
 if($this-is_valid = 
 preg_match('/^(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})$/',$mac,$parts)){
   array_shift($parts); //lose the first bit
   $this-mac = implode(':',$parts);
 }
   }
 }

 //test
 $mac_list = 
 array(00-aa-11-bb-22-cc,00:aa:11:bb:22:cc,zz:00:11:22:ff:xx,00 aa 11 
 bb 22 cc);

 foreach($mac_list as $mac){
   $mactest = new mac($mac);
   echo In:$mac;
   if($mactest-is_valid){
 echo  valid $mactest-mac\n;
   }else{
 echo  NOT valid\n;
   }
 }

$mactest  = new mac(there are a few gotchas for anyone using this);
print $mactest-is_valid ? valid\n : invalid\n;

// valid

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-27 Thread Richard Heyes

Jochem Maas wrote:
 gonna jump on your thread there Jasper, I would
 like to comment on your function and ask you a question:

 which is 'better' (for what), preg_*() or ereg[i]*()?

preg_*, for anything. They're faster, and more versatile.

--
Richard Heyes
http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-27 Thread Jochem Maas

Richard Heyes wrote:

Jochem Maas wrote:
  gonna jump on your thread there Jasper, I would
  like to comment on your function and ask you a question:
 
  which is 'better' (for what), preg_*() or ereg[i]*()?

preg_*, for anything. They're faster, and more versatile.


cool cheers.

I guess your the same Richard Heyes of 'TreeMenu' class 'fame'?
still use that very often :-)





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-27 Thread Richard Heyes

Jochem Maas wrote:

Richard Heyes wrote:


Jochem Maas wrote:
  gonna jump on your thread there Jasper, I would
  like to comment on your function and ask you a question:
 
  which is 'better' (for what), preg_*() or ereg[i]*()?

preg_*, for anything. They're faster, and more versatile.



cool cheers.

I guess your the same Richard Heyes of 'TreeMenu' class 'fame'?
still use that very often :-)


Dunno about fame, some would say Infamy, but yes, the very same.

--
Richard Heyes
http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-27 Thread Jasper Bryant-Greene
On Thu, 2005-10-27 at 00:00 +0200, Jochem Maas wrote:
 gonna jump on your thread there Jasper, I would
 like to comment on your function and ask you a question:
 
 which is 'better' (for what), preg_*() or ereg[i]*()?

I prefer preg_*(), but I used eregi() because I couldn't be bothered
figuring out his regexp (it looked right, and it worked) and that's what
he used :)

It's a matter of personal taste, mainly.

[snip]
  ?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}$,
 
 this string concatenation looks a bit naff.
 although on second thoughts I can see why you did it this way.
 down to personal choice :-).

I don't like it either, but that bit already worked, so I left it
functionally as-is, and I had to make it shorter so as to not wrap
stupidly in the email.

 the use of double quotes means strictly speaking
 you should be escaping the backslash and dollar sign (and the
 parentheseses?) although i would recommend single quotes.

Agreed.

 I would have used preg_match(), something like
[snip]

So would I, if I had been writing the function from scratch.

 white space is nice but too many blank lines lead to overscroll ;-)

Another matter of personal taste. I love whitespace and my scroll wheel
has its acceleration turned up very high, so scrolling doesn't really
bother me :)

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: [EMAIL PROTECTED]
w: http://www.album.co.nz/
b: http://jbg.name/
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



Re: [PHP] regex and global vars problem

2005-10-27 Thread Tom Rogers
Hi,

Thursday, October 27, 2005, 3:15:30 AM, you wrote:
JG I am having a problem with a couple of function I have written to check
JG for a type of string, attempt to fix it and pass it back to the main
JG function.  Any help is appreciated.

I would do it with a small class like this:

?php
class mac{
  var $mac='';
  var $is_valid = false;
  function mac($mac){
$mac = preg_replace('/[^0-9A-F]/','',strtoupper($mac));
if($this-is_valid = 
preg_match('/^(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})$/',$mac,$parts)){
  array_shift($parts); //lose the first bit
  $this-mac = implode(':',$parts);
}
  }
}

//test
$mac_list = 
array(00-aa-11-bb-22-cc,00:aa:11:bb:22:cc,zz:00:11:22:ff:xx,00 aa 11 bb 
22 cc);

foreach($mac_list as $mac){
  $mactest = new mac($mac);
  echo In:$mac;
  if($mactest-is_valid){
echo  valid $mactest-mac\n;
  }else{
echo  NOT valid\n;
  }
}

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jasper Bryant-Greene
On Wed, 2005-10-26 at 11:15 -0600, Jason Gerfen wrote:
 I 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?

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: [EMAIL PROTECTED]
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



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jason Gerfen

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: $mac1br;
   }

   /* 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: $mac1br;
   }
  
/* 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: $mac1br;
}
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:
 

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

 




--
Jason Gerfen

My girlfriend threated to
leave me if I went boarding...
I will miss her.
~ DIATRIBE aka FBITKK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jasper Bryant-Greene
On Wed, 2005-10-26 at 12:07 -0600, Jason Gerfen wrote:
 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.

OK, you've told us what your code does. Now can you explain what the
problem is? A new regex which wasn't working is a bit vague.

* Does it throw an error?
  - If so, can we have the error message?

* Does it mark invalid strings as valid?
  - Or vice versa?

Just posting a pile of code with an explanation of what it does and
leaving the rest of us to figure out what the problem is, is not helping
us to help you.

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: [EMAIL PROTECTED]
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



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jason Gerfen
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.


Jasper Bryant-Greene wrote:


On Wed, 2005-10-26 at 12:07 -0600, Jason Gerfen wrote:
 


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.
   



OK, you've told us what your code does. Now can you explain what the
problem is? A new regex which wasn't working is a bit vague.

* Does it throw an error?
 - If so, can we have the error message?

* Does it mark invalid strings as valid?
 - Or vice versa?

Just posting a pile of code with an explanation of what it does and
leaving the rest of us to figure out what the problem is, is not helping
us to help you.

 




--
Jason Gerfen

My girlfriend threated to
leave me if I went boarding...
I will miss her.
~ DIATRIBE aka FBITKK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jasper Bryant-Greene
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: [EMAIL PROTECTED]
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



Re: [PHP] regex and global vars problem

2005-10-26 Thread Jochem Maas

gonna jump on your thread there Jasper, I would
like to comment on your function and ask you a question:

which is 'better' (for what), preg_*() or ereg[i]*()?

Jasper Bryant-Greene wrote:

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}$,


this string concatenation looks a bit naff.
although on second thoughts I can see why you did it this way.
down to personal choice :-).

the use of double quotes means strictly speaking
you should be escaping the backslash and dollar sign (and the
parentheseses?) although i would recommend single quotes.

I would have used preg_match(), something like
(I'm using double quotes because of the php -r '' btw :-):

php -r '

$d  = ([0-9A-Fa-f]{2});
$q  = {$d}[\\-:]?;
$re = #^{$q}{$q}{$q}{$q}{$q}{$d}\$#;
foreach(array(900030ABAB0C,
  90-00-30-AB-AB-0C,
  90:00:30:AB:AB:0C,
  90-00-30:AB:AB-0C,
  90:JJ:30:AB:AB-0C) as $str) {
$matches = array();
if (preg_match($re, $str, $matches)) {
$mac = join(:, array_slice($matches, 1));
echo good mac: $mac\n;
} else {
echo bad mac: $str\n;
}
}

// and as a function:

function getMAC($str)
{
static $re;
if (!isset($re)) {
$d  = ([0-9A-Fa-f]{2}); $q = {$d}[\\-:]?;
$re = #^{$q}{$q}{$q}{$q}{$q}{$d}\$#;
}

return (preg_match($re, $str, $matches)) ? join(:, array_slice($matches, 
1)) : null;
}

foreach(array(900030ABAB0C,
  90-00-30-AB-AB-0C,
  90:00:30:AB:AB:0C,
  90-00-30:AB:AB-0C,
  90:JJ:30:AB:AB-0C) as $str) {
if ($mac = getMAC($str)) {
echo good mac: $mac\n;
} else {
echo bad mac: $str\n;
}
}

'


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


white space is nice but too many blank lines lead to overscroll ;-)



}
?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php