PHP preg match all() 맥주소 추출

PHP preg_match_all 맥주소 추출

1 예시 1[ | ]

function extract_mac_address($str) {
	preg_match_all("/([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}/", $str, $matches);
	return $matches[0];
}
$ifconfig_result = "eth0      Link encap:Ethernet  HWaddr 52:54:00:5D:DB:01
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:982 errors:0 dropped:0 overruns:0 frame:0
          TX packets:970 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:92188 (90.0 KiB)  TX bytes:91468 (89.3 KiB)
eth1      Link encap:Ethernet  HWaddr 52:54:00:5D:DB:02
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:982 errors:0 dropped:0 overruns:0 frame:0
          TX packets:970 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:92188 (90.0 KiB)  TX bytes:91468 (89.3 KiB)";
$mac_addr_arr = extract_mac_address( $ifconfig_result );
print_r( $mac_addr_arr );

2 예시 2[ | ]

function extract_mac_address($str) {
	preg_match_all("/([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}/", $str, $matches);
	return $matches[0];
}
exec("ifconfig", $output);
$ifconfig_result = implode("\n", $output);
$mac_addr_arr = extract_mac_address( $ifconfig_result );
print_r( $mac_addr_arr );
# Array
# (
#     [0] => 68:0A:24:79:1C:35
# )

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}