Page 1 of 1
Mac Address insert colon
Posted: Wed Oct 03, 2007 9:15 pm
by mgichoga
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
Posted: Wed Oct 03, 2007 9:46 pm
by feyd
I'd probably use
str_split() instead.
What have you tried as far as regular expressions... (if you wish to continue this route) ?
Posted: Wed Oct 03, 2007 10:13 pm
by VladSun
I would follow feyd's suggestion by using
str_split() as an argument for
implode()
Posted: Wed Oct 03, 2007 10:36 pm
by mgichoga
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";
Posted: Wed Oct 03, 2007 10:38 pm
by feyd
substr() is available, but the function I already linked to is more useful.
Posted: Wed Oct 03, 2007 11:34 pm
by VladSun
Did you look at the functions feyd and me pointed to?
Posted: Wed Oct 03, 2007 11:43 pm
by GeertDD
And if you really wanted to use a regex, try one like this:
Code: Select all
preg_replace('/..(?!$)/', '$0:', '0030488400C8')
Posted: Thu Oct 04, 2007 1:25 am
by mrkite
edit: never mind, GeertDD beat me to it.
Posted: Thu Oct 04, 2007 11:37 am
by mgichoga
Thanks to all of ya.
Appreciate it.