FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Regarding split text and match from data base
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Regarding split text and match from data base [message #183945 is a reply to message #183943] Thu, 28 November 2013 16:01 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)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(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: creating key / hash
Next Topic: from mysql in associative array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 05:29:56 GMT 2024

Total time taken to generate the page: 0.04127 seconds