Mac Address insert colon
Moderator: General Moderators
Mac Address insert colon
Hi,
I'm seeking help with a peculiar problem. i assigned some clerical staff to enter some computer inventoy and in their wisdom they ommitted the colon in the mac addresses. I have over 2000 records and I'm fairly a novice in regex and was wondering if there is a quick way to insert a colon so it could match a normal mac address e.g
0030488400C8 to 00:30:48:84:00:C8
Any help will be greatly appreciated
I'm seeking help with a peculiar problem. i assigned some clerical staff to enter some computer inventoy and in their wisdom they ommitted the colon in the mac addresses. I have over 2000 records and I'm fairly a novice in regex and was wondering if there is a quick way to insert a colon so it could match a normal mac address e.g
0030488400C8 to 00:30:48:84:00:C8
Any help will be greatly appreciated
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I'd probably use str_split() instead.
What have you tried as far as regular expressions... (if you wish to continue this route) ?
What have you tried as far as regular expressions... (if you wish to continue this route) ?
I would follow feyd's suggestion by using str_split() as an argument for implode()
There are 10 types of people in this world, those who understand binary and those who don't
now that you mentioned I remembered there is a substr function in perl. This is what I ended up doing in perl. Obviously I know there is a better way of doing this than this method so any suggestions in regex/php/perl are welcome
$mac="002145783456";
$mac1=substr($mac,0,2);
$mac2=substr($mac,4,2);
$mac3=substr($mac,6,2);
$mac4=substr($mac,8,2);
$mac5=substr($mac,10,2);
$pad="$mac1:"."$mac2:"."$mac3:"."$mac4:"."$mac5";
print "$mac\n";
print "$pad\n";
$mac="002145783456";
$mac1=substr($mac,0,2);
$mac2=substr($mac,4,2);
$mac3=substr($mac,6,2);
$mac4=substr($mac,8,2);
$mac5=substr($mac,10,2);
$pad="$mac1:"."$mac2:"."$mac3:"."$mac4:"."$mac5";
print "$mac\n";
print "$pad\n";
And if you really wanted to use a regex, try one like this:
Code: Select all
preg_replace('/..(?!$)/', '$0:', '0030488400C8')