Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12004

Re: Regarding split text and match from data base

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: Regarding split text and match from data base
Date 2013-11-28 11:01 -0500
Organization A noiseless patient Spider
Message-ID <l77pcm$uih$1@dont-email.me> (permalink)
References (1 earlier) <2ca0dc82-85a2-4e57-b6b8-ada747640289@googlegroups.com> <l723lj$ugf$2@dont-email.me> <b9bfb809-4513-4262-a0c9-65acf113aa43@googlegroups.com> <l7518b$e05$1@dont-email.me> <0.df047f3bb244630b50f4.20131128122727GMT.87k3fs1zkg.fsf@bsb.me.uk>

Show all headers | View raw


On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex@attglobal.net> writes:
> <snip>
>> Here is a better version.  It uses a regex to get all of the ip and
>> mac addresses in an array, then selects them.  I set this up for CLI
>> output (for basic testing here), so you'll have to change the display.
>> But otherwise it should work with few modifications and correcting for
>> the line wrapping as indicated
>>
>> <?php
>>
>> $arp = shell_exec('arp -a');
>>
>> ----
>>
>> // *1
>> $res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2})/m',
>> $arp, $matches, PREG_SET_ORDER);
>
> I'd do this a little differently.  I'd use PREG_PATTERN_ORDER so that I
> can just loop through $matches[2] without any further subscripting, but
> I'd also not capture the IP address since it does not seem to be used.
>

The OP indicated he wanted to capture the IP address.  We don't know 
what else he may be using it for.  And I used PREG_SET_ORDER 
specifically to keep the IP address with the MAC address.

> A few other simplifications include using the i modifier rather than
> writing a-fA-F, using \d rather than [0-9], not quoting '-' and using a
> non-capturing group to specify the repeats:
>

All good ideas.  I was trying to keep it as simple as possible for the 
OP's benefit.

>   $res = preg_match_all(
>      '/^\s*\d{1,3}(?:\.\d{1,3}){3}\s+([\dA-F]{2}(?:-[\dA-F]{2}){5})/mi',
>      $arp, $matches, PREG_PATTERN_ORDER);
>
> As a result of changing the capture order, I'd also be tempted to
> re-write the loop:
>
> <snip>
>>        $macs = ''; // *4
>>        foreach ($matches as $match) {
>>           if ($macs != '') // Not the first time through, so
>>              $macs .= ', '; // Add a separator
>>           $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>> . '\'';  // Append the next MAC address (again all one line above)
>>        }
>
> as $macs = "'" . join("', '", $matches[1]) . "'";
>

Which doesn't work when you capture both MAC and IP.  Additionally, you 
don't call mysqli_real_escape_string() for the values.  Yes, I know it's 
not actually required here - but it's a good habit for new programmers 
to get into.  It's much better to call it when not needed than to not 
call it when it is needed.

> I don't claim any of these changes make the code better (except,
> perhaps, the grouping in the regexp) but I think showing alternatives is
> always a good thing.
>
> <snip>
>

No argument.  All are good ideas if they fit into the OP's needs.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-22 03:28 -0800
  Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-22 09:24 -0500
  Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-22 16:34 +0100
    Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-22 10:54 -0500
      Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-22 17:50 +0100
        Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-22 13:57 -0500
          Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-23 08:52 +0100
            Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-23 07:05 -0500
              Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-24 17:17 +0100
                Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-24 11:23 -0500
  Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-24 20:02 -0800
  Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-24 22:09 -0800
  Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-24 22:35 -0800
    Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-25 08:35 +0100
      Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-25 08:36 +0100
    Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-25 09:31 -0500
  Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-25 21:32 -0800
  Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-25 21:41 -0800
    Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-26 07:20 -0500
      Re: Regarding split text  and match from data base jalaf28@gmail.com - 2013-11-26 21:22 -0800
        Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-27 12:29 +0100
          Re: Regarding split text  and match from data base Arno Welzel <usenet@arnowelzel.de> - 2013-11-27 12:31 +0100
        Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-27 09:57 -0500
          Re: Regarding split text  and match from data base Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-28 12:27 +0000
            Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-28 11:01 -0500
              Re: Regarding split text  and match from data base Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-28 18:51 +0000
                Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-28 19:27 -0500
                Re: Regarding split text  and match from data base Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-29 02:34 +0000
                Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-28 22:47 -0500
                Re: Regarding split text  and match from data base Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-29 05:01 +0000
                Re: Regarding split text  and match from data base Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-29 07:40 -0500
                Re: Regarding split text  and match from data base Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-29 13:58 +0000

csiph-web