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


Groups > comp.lang.php > #12014

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 22:47 -0500
Organization A noiseless patient Spider
Message-ID <l792o6$ko8$1@dont-email.me> (permalink)
References (5 earlier) <0.df047f3bb244630b50f4.20131128122727GMT.87k3fs1zkg.fsf@bsb.me.uk> <l77pcm$uih$1@dont-email.me> <0.f78e527401699a6f7ed5.20131128185119GMT.87txewz7fc.fsf@bsb.me.uk> <l78n12$7d9$1@dont-email.me> <0.9d698dd369d91728826e.20131129023447GMT.87a9goylyw.fsf@bsb.me.uk>

Show all headers | View raw


On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex@attglobal.net> writes:
>
>> On 11/28/2013 1:51 PM, Ben Bacarisse wrote:
>>> Jerry Stuckle <jstucklex@attglobal.net> writes:
>>>
>>>> On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
> <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.
>
> It parses it, it just doesn't capture it.  When you capture both MAC and
> IP (to use your exact phrase) you just replace 1 with 2.  Am I missing
> your point?
>

Ah, OK, I understand you now.  But it still doesn't work if you use set 
order.

>>   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.
>
> We should agree to disagree on this.  I can think of ways in which the
> IP address might be used where pattern order is still better (for
> example, building another query but using IP addresses this time) but
> there are also cases where set order will be more logical or helpful.
> However, I made the suggestion based on what your code actually did.
>

My code was based on the OP's overall requirements.  This is just a 
small part of the whole script, I believe.

> Going back to the OP's code I see that he or she wants to print the IP
> as well.  Your loop was:
>
>    while ($data = mysqli_fetch_row($res))
>        echo $data['mac_id'] . '  ' . $data['mac_name'] . "\n";
>
> and we can add the IP address just as easily with either capture
> ordering.
>

Very easy to do with set order.  Each pair is matched up - ip address in 
element 1 and mac address in element 2 of the array.  It is very easy to 
use foreach() to go through the base array and access element 1 and 
element 2 of the internal array, as I did.  If you use pattern order, 
you can't do that.

But I think we are arguing a matter of style here.  I also use pattern 
order when appropriate.  In this case I still think set order is more 
appropriate.

>>>>    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.
>
> I think we are talking at cross purposes.  You mean (I think) that it's
> never wrong to call it when it might be right to call it -- and that's
> true.  I took your "better to call it when not needed" absolutely
> literally.  For example, if the string is already quoted it is "not
> needed" and calling it will break something.
>

No, if the string is already quoted, it will not "break something". 
Only if the string has already been processed through 
mysqli_real_escape_string() could there be a problem.  Any other method 
of "quoting" is not supported.

And yes, I will still recommend using mysqli_real_escape_string(), even 
when not needed (at least right now it's not needed).

And yes, when working with people unfamiliar with a language (here or in 
my classes), I ALWAYS go on the side of caution.  It keeps people out of 
trouble more often than not.  They learn the conservative rules, and 
later learn when it is OK to break those rules - rather than vice versa.

-- 
==================
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