?
Solved

String search

Posted on 2006-06-01
2
Medium Priority
?
302 Views
Last Modified: 2008-03-03
I have some code stored in a variable that is the output of "ipconfig/all" from a dos command prompt.

I can search through it line by line, but I need to search for a pattern of letters equal to a Mac address of a computer network card.

The code I use at trhe moment is:

// look for 'Physical Address'
foreach ( $lines as $line )
{
  if ( substr($line, 0, 16) == 'Physical Address' )
  {
      $macs[] = substr($line, strlen($line)-17);
  }
}

This code works, but only on english versions of Microsoft. I need it to check it on all language versions of Microsoft.  How can I search for the MAC address string using PHP regular expressions.  I need to search every line in the loop above for the format of characters:

??-??-??-??-??-??

Where ? can be any charachter (so it is six sets of two question marks separated by a - ).

Can you help ?!

Thanks

Ashley :-)
0
Comment
Question by:jbclelland
2 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16809903
if(preg_match('/([0-9A-H]{2}(\-[0-9A-H]{2}){5})/', $line, $match)) {
  $macAddress = $line[1];
}

-r-
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1000 total points
ID: 16814835
<?php
$line = 'Windows IP Configuration

        Host Name . . . . . . . . . . . . : richardquadling
        Primary Dns Suffix  . . . . . . . : MY_DOMAIN
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No
        DNS Suffix Search List. . . . . . : MY_DOMAIN

Ethernet adapter Onboard LAN Connection:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connection
        Physical Address. . . . . . . . . : 00-0C-F1-E4-70-29
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 10.0.0.19
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.0.7
        DNS Servers . . . . . . . . . . . : 10.0.0.7';
       
if(preg_match_all('/([0-9A-H]{2}(?:-[0-9A-H]{2}){5})/sim', $line, $matches))
      {
      print_r($matches);
      }
?>
Array
(
    [0] => Array
        (
            [0] => 00-0C-F1-E4-70-29
        )

    [1] => Array
        (
            [0] => 00-0C-F1-E4-70-29
        )

)

Slight change to Roonaan's regexp.

And for multiple network cards (just running this ipconfig from one of our servers) ...

<?php
$line = 'Windows 2000 IP Configuration

        Host Name . . . . . . . . . . . . : EXCHANGE
        Primary DNS Suffix  . . . . . . . : MY_DOMAIN
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : Yes
        WINS Proxy Enabled. . . . . . . . : No
        DNS Suffix Search List. . . . . . : MY_DOMAIN

Ethernet adapter Team #0 - ALB Mode:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : Intel(R) Advanced Network Services Virtual Adapter
        Physical Address. . . . . . . . . : 00-06-5B-FE-AE-EF
        DHCP Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 10.0.0.5
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        IP Address. . . . . . . . . . . . : 10.0.0.7
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :
        DNS Servers . . . . . . . . . . . : 10.0.0.7
        Primary WINS Server . . . . . . . : 10.0.0.7

Ethernet adapter ONBOARD ADSL 1000 (2):

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet #2
        Physical Address. . . . . . . . . : 00-06-5B-FE-AE-F0
        DHCP Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.0.1
        DNS Servers . . . . . . . . . . . : 10.0.0.7
        Primary WINS Server . . . . . . . : 10.0.0.7

PPP adapter RAS Server (Dial In) Interface:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface
        Physical Address. . . . . . . . . : 00-53-45-00-00-00
        DHCP Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 10.0.0.45
        Subnet Mask . . . . . . . . . . . : 255.255.255.255
        Default Gateway . . . . . . . . . :
        DNS Servers . . . . . . . . . . . : 127.0.0.1';
       
if(preg_match_all('/([0-9A-H]{2}(?:-[0-9A-H]{2}){5})/sim', $line, $matches))
      {
      print_r($matches);
      }
?>

results in ...

Array
(
    [0] => Array
        (
            [0] => 00-06-5B-FE-AE-EF
            [1] => 00-06-5B-FE-AE-F0
            [2] => 00-53-45-00-00-00
        )

    [1] => Array
        (
            [0] => 00-06-5B-FE-AE-EF
            [1] => 00-06-5B-FE-AE-F0
            [2] => 00-53-45-00-00-00
        )

)

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question