|
Posted by Jochem Maas on 10/20/14 11:30
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 ;-)
>
> }
> ?>
>
[Back to original message]
|