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 #183955 is a reply to message #183952] Fri, 29 November 2013 02:34 Go to previous messageGo to previous message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma:
Member
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> 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:
<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?

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

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.

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

--
Ben.
[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: Sat Nov 30 11:08:04 GMT 2024

Total time taken to generate the page: 0.07402 seconds