PHP preg match all() 맥주소 추출

Jmnote (토론 | 기여)님의 2016년 4월 10일 (일) 16:03 판 (→‎같이 보기)
PHP preg_match_all 맥주소 추출

1 예시 1

PHP
Copy
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 );
/*
Array
(
    [0] => 52:54:00:5D:DB:01
    [1] => 52:54:00:5D:DB:02
)
*/

2 예시 2

PHP
Copy
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 같이 보기