Format MAC address - Desprate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkubena
Forum Newbie
Posts: 3
Joined: Wed Mar 04, 2009 2:14 pm

Format MAC address - Desprate

Post by jkubena »

all i want to do is format an array of these
0:20:d6:83:57:95

and turn it into an array of these
00 20 D6 83 57 95

i need one string formatted like the other or visa versa.
this will allow me to compare a port to mac address table with an IP to mac address table to figure out the IP of the device on the port.

Please help, im begging you!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Format MAC address - Desprate

Post by VladSun »

Take a look at explode() function
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Format MAC address - Desprate

Post by VladSun »

jkubena wrote:this will allow me to compare a port to mac address table with an IP to mac address table to figure out the IP of the device on the port.
Could you explain this in more details? Where do you get these ports/MAC addresses from?
There are 10 types of people in this world, those who understand binary and those who don't
jkubena
Forum Newbie
Posts: 3
Joined: Wed Mar 04, 2009 2:14 pm

Re: Format MAC address - Desprate

Post by jkubena »

i do a shell exec and run an SNMP query, some of the macs come back formatted differently. i do know how to use the explode function very well, i tried exploding the mac address and then searching the string array and if the string length is 1 prepend a 0 on it, then implode it back to mac address form. but php is a retarded language and it doesn't know the difference between a string and an int so it just adds a zero to it sometimes, sometimes it works, other times it doesn't.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Format MAC address - Desprate

Post by pickle »

Um... you're on a PHP forum - calling PHP a retarded language isn't likely going to help you make friends here :roll:

If you've got an array of those strings, just loop through those strings & do a str_replace() to replace ':' with ' ', then run them through strtoupper()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jkubena
Forum Newbie
Posts: 3
Joined: Wed Mar 04, 2009 2:14 pm

Re: Format MAC address - Desprate

Post by jkubena »

sorry im a bit frustrated. but i also need to add zeros too, i would be really easy if thats all i had to do, i wanna to like a $str = 0$str if the str length is one after i explode by ":". but all it does it add the two numbers together.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Format MAC address - Desprate

Post by pickle »

You concatenate strings with "."

Code: Select all

$a = 'A';
$b = 'B'
$c = $a.$b;
 
//$c now equals AB
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply