Re: Regarding split text and match from data base [message #183952 is a reply to message #183950] |
Fri, 29 November 2013 00:27 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 11/28/2013 1:51 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> 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.
>
> Without more code it's hard to tell, but for what I was commenting on,
> pattern order looks better. The only advantage of either order is if
> there is some natural operation that can be performed on $matches[i] and
> there is no evidence for that as yet for 'set' order. There is a
> natural operation that is done on all the macs, which it why, on the
> balance of the current evidence, I'd favour pattern order.
>
Yes, it is hard to tell without more code. However, lacking that I
think set order is more appropriate, because the linkage between the ip
and the mac address is maintained.
> <snip>
>>> 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.
>
> You can if you just write 2 instead of 1, but I suspect that's not what
> you meant. You can't do this if you collect in 'set' order rather than
> 'pattern' order, but even if the IP address is captured there's no clear
> reason to choose set order (so far as we know).
>
No, because your regex did not parse out the ip. And again, pattern
order breaks the linkage between ip and mac address. I chose that
because if the OP wants the ip also, it would not make sense to use it
without the mac address.
>> 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.
>
> Well, you can't mean that literally (there are places where it's not
> needed where using it would break the code) but I know what you are
> getting at. Part of the equation should be, in my view, how likely it
> is that the quoting will be needed. Given the context (mac addresses)
> it's not very likely, and I'd take that into account.
>
> <snip>
>
Yes, I do mean that literally. And there is no place where you call it
for strings that will break the code. If there is, I would like you to
show it. mysqli_real_escape_string is about more than quoting.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|