Dinesh G
Dinesh G

Reputation: 1365

How to Filter Mac Address from Specified String using Regex in PHP?

Actually i have the String Like Following?

Physical Address Transport Name =================== 
==========================================================
00-1F-29-A8-CA-54 Media disconnected 
00-21-5C-68-BB-17 \Device\Tcpip_{1A76BCB8-6BD0-45AA-85B0-3016C3F82A5B}

Please Help me to to filter only mac address from the above string. Thanks you.

Upvotes: 2

Views: 1562

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Solution using preg_match_all function and POSIX character class [:xdigit:](will match all hexadecimal digits):

// $str is your initial string
preg_match_all("/\b[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}\b/su", $str, $matches);

print_r($matches[0]);

The output:

Array
(
    [0] => 00-1F-29-A8-CA-54
    [1] => 00-21-5C-68-BB-17
)

Upvotes: 2

Related Questions