Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #11915 > unrolled thread
| Started by | jalaf28@gmail.com |
|---|---|
| First post | 2013-11-22 03:28 -0800 |
| Last post | 2013-11-29 13:58 +0000 |
| Articles | 12 on this page of 32 — 4 participants |
Back to article view | Back to comp.lang.php
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Date | 2013-11-27 12:29 +0100 |
| Message-ID | <5295D781.8090403@arnowelzel.de> |
| In reply to | #11994 |
Am 27.11.2013 06:22, schrieb jalaf28@gmail.com:
> here is the Code..
>
>
> <?php
>
> $out=shell_exec("arp -a");
>
> $ex=explode(" ",$out);
>
> $j=21;
> $k=32;
>
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> die("Could not Connect to The Server");
> }
>
> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> $rs=mysql_query("select * from mac_add");
>
>
>
> while($rr=mysql_fetch_array($rs))
> {
> $rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
> while( $res=mysql_fetch_array($rsr))
> {
>
> echo "<tr><td>" . htmlspecialchars($ex[$j]) . "</td><td>" . htmlspecialchars($res[0]) . "</td> </td><td>" . htmlspecialchars($res[1]) . "</td></tr>" ;
>
> }
>
> $j +=21;
> $k +=21;
>
> }
This makes no sense - you first select ALL records which you then don't
use at all.
You also assume that every 21th character a new output line of arp
begins - an very insecure assumption and even more worse you loop
through the number of records in your mac_add table - so you assume
there are never more output lines in arp -a than records in your
database. Therefore if arp will output 15 lines and your table only has
10 records you would always miss 5 lines - since the loop only handles
the 10 records.
If you want to make sure you get a result for every output line from
"arp -a" you should split the lines of the output and loop through that
and not the database records.
This is just a quick & dirty hack and may contain typos - but you may
get the idea:
<?php
$ln=@mysql_connect("localhost","root","");
if(!$ln)
{
// TODO: Gracefully error handling - and not just
// terminating the script
die("Could not Connect to the database");
}
echo "<table>";
// Execute "arp -a" with the output lines in the $output array
// and the return code of the arp command in $returncode
exec("arp -a", $output, $returncode);
// TODO: Check $returncode and handle errors
// Loop through all output lines from the arp command
foreach($output as $line)
{
// TODO: Check, if $line contains an IP/Mac address at all
$elements = explode(" ", $line);
// Build query to look up the entry for a given MAC address
$query = "select * from mac_add where mac_id='".
mysql_real_escape_string($elements[32])."'";
// Execute query
$recordset = mysql_query($query);
// Fetch result and output it if there is one
if($record = mysql_fetch_array($recordset))
{
echo "<tr>" .
"<td>" . htmlspecialchars($elements[21]) . "</td>" .
"<td>" . htmlspecialchars($record[0]) . "</td>" .
"<td>" . htmlspecialchars($record[1]) . "</td>" .
"</tr>";
}
}
echo "</table>";
mysql_close();
?>
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
[toc] | [prev] | [next] | [standalone]
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Date | 2013-11-27 12:31 +0100 |
| Message-ID | <5295D7F7.7080308@arnowelzel.de> |
| In reply to | #11996 |
Am 27.11.2013 12:29, schrieb Arno Welzel:
> Am 27.11.2013 06:22, schrieb jalaf28@gmail.com:
>
[...]
>> $j +=21;
>> $k +=21;
>>
>> }
>
> This makes no sense - you first select ALL records which you then don't
> use at all.
>
> You also assume that every 21th character a new output line of arp
Of course "element", not "character"...
> begins - an very insecure assumption and even more worse you loop
> through the number of records in your mac_add table - so you assume
> there are never more output lines in arp -a than records in your
> database. Therefore if arp will output 15 lines and your table only has
> 10 records you would always miss 5 lines - since the loop only handles
> the 10 records.
>
> If you want to make sure you get a result for every output line from
> "arp -a" you should split the lines of the output and loop through that
> and not the database records.
>
> This is just a quick & dirty hack and may contain typos - but you may
> get the idea:
>
> <?php
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> // TODO: Gracefully error handling - and not just
> // terminating the script
> die("Could not Connect to the database");
> }
>
> echo "<table>";
>
> // Execute "arp -a" with the output lines in the $output array
> // and the return code of the arp command in $returncode
> exec("arp -a", $output, $returncode);
>
> // TODO: Check $returncode and handle errors
>
> // Loop through all output lines from the arp command
> foreach($output as $line)
> {
> // TODO: Check, if $line contains an IP/Mac address at all
> $elements = explode(" ", $line);
>
> // Build query to look up the entry for a given MAC address
> $query = "select * from mac_add where mac_id='".
> mysql_real_escape_string($elements[32])."'";
>
> // Execute query
> $recordset = mysql_query($query);
>
> // Fetch result and output it if there is one
> if($record = mysql_fetch_array($recordset))
> {
> echo "<tr>" .
> "<td>" . htmlspecialchars($elements[21]) . "</td>" .
> "<td>" . htmlspecialchars($record[0]) . "</td>" .
> "<td>" . htmlspecialchars($record[1]) . "</td>" .
> "</tr>";
> }
> }
>
> echo "</table>";
>
> mysql_close();
> ?>
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-27 09:57 -0500 |
| Message-ID | <l7518b$e05$1@dont-email.me> |
| In reply to | #11994 |
On 11/27/2013 12:22 AM, jalaf28@gmail.com wrote:
> Hi Jerry
>
> i already Post It
> but now i m post it again..
>
> here is the Code..
>
>
> <?php
>
> $out=shell_exec("arp -a");
>
> $ex=explode(" ",$out);
>
> $j=21;
> $k=32;
>
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> die("Could not Connect to The Server");
> }
>
> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> $rs=mysql_query("select * from mac_add");
>
>
>
> while($rr=mysql_fetch_array($rs))
> {
> $rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
> while( $res=mysql_fetch_array($rsr))
> {
>
> echo "<tr><td>" . htmlspecialchars($ex[$j]) . "</td><td>" . htmlspecialchars($res[0]) . "</td> </td><td>" . htmlspecialchars($res[1]) . "</td></tr>" ;
>
> }
>
> $j +=21;
> $k +=21;
>
> }
>
>
> echo "</table></center>";
>
> mysql_close();
>
> ?>
>
OK, your code was posted in another part of the thread, so I wasn't sure
if that was what you were currently using or not.
There are several problems with your code:
1. Don't use '@' in front of function calls. It prevents you from
seeing any errors (even in the log). Rather, your development system
should have display_errors=on in your php.ini file, and your production
system should have display_errors=off.
2. The mysql extension has been deprecated. New code should use the
mysqli extension.
3. You shouldn't use die() in your web scripts. This terminates ALL
processing of your code, including any other html on the page. The
results is either a completely empty page (if your PHP code is at the
start of your page) or invalid HTML. Neither is good. Rather, if the
call fails, use a conditional to go around the code that relies on the
failing command.
4. You should ALWAYS have a root password on your database, even on your
local system.
5. The 'mysql' database is a system database and may be replaced or
changed by any upgrade. You should NEVER use it for your own data.
Create your own database instead.
6. You are depending on absolute positions for the start of your data.
This is almost never good.
7. You fetch the entire table, then loop through the entries but never
use them. Rather, you use this loop to execute additional SELECT
statements.
8. You make multiple calls to SELECT individual mac addresses. This is
less efficient than doing it all in one call.
9. You are not escaping the mac address before using it in the mysql
call. While it's not going to matter here (because the regex does
filtering), it's almost always good to use on strings for safety reasons.
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);
// (the preceding should be all one line)
if ($res === false)
echo "Regex error!\n";
else { // *2
if ($link = mysqli_connect("localhost","root","password", "test")) {
// *3
$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)
}
if ($macs != '') { // Ensure we actually had some *5
$sql = "SELECT mac_id, mac_name FROM mac_addr WHERE mac_id IN
($macs)"; // one line here also
$res = mysqli_query($link, $sql);
if ($res) { // .. If it worked
while ($data = mysqli_fetch_row($res))
echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";
}
else
echo "SELECT error: " . mysqli_error($link) . "\n";
}
}
else
echo "Connect error: " . mysqli_connect_error() . "\n";
}
?>
Notes:
1. This regex looks much worse than it is
2. $matches will be an array of arrays
Each array within $matches will contain:
[0] IP MAC_ADDR
[1] IP
[2] MAC_ADDR
3. Use mysqli instead of deprecated mysql.
You can also specify the database here (NOT mysql!)
And ensure you have a password
4. We could just issue multiple SELECT statements, one for each MAC
address. However, this is inefficient. Better is to use the IN
clause to
specify all MAC addresses in one SQL statement
5. $macs will now contain: 'mac1', 'mac2', 'mac3', ...' macx'
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-11-28 12:27 +0000 |
| Message-ID | <0.df047f3bb244630b50f4.20131128122727GMT.87k3fs1zkg.fsf@bsb.me.uk> |
| In reply to | #11998 |
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.
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:
$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]) . "'";
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>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-28 11:01 -0500 |
| Message-ID | <l77pcm$uih$1@dont-email.me> |
| In reply to | #12002 |
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
==================
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-11-28 18:51 +0000 |
| Message-ID | <0.f78e527401699a6f7ed5.20131128185119GMT.87txewz7fc.fsf@bsb.me.uk> |
| In reply to | #12004 |
Jerry Stuckle <jstucklex@attglobal.net> writes:
> 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.
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.
<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).
> 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>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-28 19:27 -0500 |
| Message-ID | <l78n12$7d9$1@dont-email.me> |
| In reply to | #12009 |
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:
>>> 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.
>
> 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@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-11-29 02:34 +0000 |
| Message-ID | <0.9d698dd369d91728826e.20131129023447GMT.87a9goylyw.fsf@bsb.me.uk> |
| In reply to | #12011 |
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?
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-28 22:47 -0500 |
| Message-ID | <l792o6$ko8$1@dont-email.me> |
| In reply to | #12013 |
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
==================
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-11-29 05:01 +0000 |
| Message-ID | <0.3fdf6273da9361217018.20131129050131GMT.8738mfztqs.fsf@bsb.me.uk> |
| In reply to | #12014 |
Jerry Stuckle <jstucklex@attglobal.net> writes:
> On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
<snip>
>> 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.
The pairing is not needed -- that's why I quoted your own code. It is
just as easy to add the IP address to your output using either order.
> 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.
Are we looking at different code? I don't see anywhere where you do
that.
You print the elements using a while statement that does not use the
matched values at all. Adding the IP address to the output can be done
as easily with either capture order. The only foreach loop I can see
was used to build list of MAC addresses and it has no need of the
corresponding IP address.
As it happens, I think that code is actually a little clearer using
pattern order. Rather than:
foreach ($matches as $match) {
if ($macs != '') // Not the first time through, so
$macs .= ', '; // Add a separator
$macs .= '\'' . mysqli_real_escape_string($link, $match[2])
. '\'';
}
you get:
foreach ($matches[2] as $match) {
if ($macs != '') // Not the first time through, so
$macs .= ', '; // Add a separator
$macs .= '\'' . X_mysqli_real_escape_string($link, $match)
. '\'';
}
where it's clear from the get-go that the loop is processing the MAC
addresses. There's no need to see what elements of $match get using in
the loop body to know what it's processing.
> 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.
As I said, we should agree to disagree. My conclusion is the reverse of
yours.
>>>>> 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.
I just don't get this. If I have some data that is already quoted it
must be inserted it into an SQL statement as is. If I pass it through
mysqli_real_escape_string it will not longer be correctly quoted.
But, anyway, you've just given an example yourself of a situation where
it is not better to cal is when not needed -- when the data has, at some
point, already been through mysqli_real_escape_string.
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-11-29 07:40 -0500 |
| Message-ID | <l7a20e$sc2$1@dont-email.me> |
| In reply to | #12015 |
On 11/29/2013 12:01 AM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex@attglobal.net> writes:
>
>> On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
> <snip>
>>> 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.
>
> The pairing is not needed -- that's why I quoted your own code. It is
> just as easy to add the IP address to your output using either order.
>
You can't easily do it with foreach(), for instance. i.e.
foreach($matches as $match) {
$ip = mysqli_real_escape_string($link, $match[1]);
$mac = mysqli_real_escape_string($link, $match[2]);
mysqli_query($link, 'INSERT INTO myTable(ip,mac) ' .
"VALUES ('$ip', '$mac')";
>> 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.
>
> Are we looking at different code? I don't see anywhere where you do
> that.
>
No, I'm not doing it here. But that doesn't mean it isn't necessary.
He wanted the IP address for a reason.
> You print the elements using a while statement that does not use the
> matched values at all. Adding the IP address to the output can be done
> as easily with either capture order. The only foreach loop I can see
> was used to build list of MAC addresses and it has no need of the
> corresponding IP address.
>
Not in this case. But that doesn't mean it isn't necessary. He wanted
the IP address for a reason.
> As it happens, I think that code is actually a little clearer using
> pattern order. Rather than:
>
> foreach ($matches as $match) {
> if ($macs != '') // Not the first time through, so
> $macs .= ', '; // Add a separator
> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
> . '\'';
> }
>
> you get:
>
> foreach ($matches[2] as $match) {
> if ($macs != '') // Not the first time through, so
> $macs .= ', '; // Add a separator
> $macs .= '\'' . X_mysqli_real_escape_string($link, $match)
> . '\'';
> }
>
AI really don't see where this is clearer. So you're not using
$matches[2] instead of in $match[2]. It is immaterial.
> where it's clear from the get-go that the loop is processing the MAC
> addresses. There's no need to see what elements of $match get using in
> the loop body to know what it's processing.
>
It's also clear from my loop that you are processing the mac addresses.
In your case you still need to know it's in $matches[2].
>> 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.
>
> As I said, we should agree to disagree. My conclusion is the reverse of
> yours.
>
Yes, and you won't give it up, will you?
>>>>>> 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.
>
> I just don't get this. If I have some data that is already quoted it
> must be inserted it into an SQL statement as is. If I pass it through
> mysqli_real_escape_string it will not longer be correctly quoted.
>
The only valid way to quote a string is with mysqli_real_escape_string()
(or the deprecated mysql_escape_string). Of course you would not expect
to get the correct results if you process a string two times though the
function.
> But, anyway, you've just given an example yourself of a situation where
> it is not better to cal is when not needed -- when the data has, at some
> point, already been through mysqli_real_escape_string.
>
> <snip>
>
Then you should already know that. That's only a matter of common
sense. But you are still processing it with mysqli_real_escape_string().
But it seems you feel the need to argue just to argue.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-11-29 13:58 +0000 |
| Message-ID | <0.bca32e23660040b2738e.20131129135831GMT.87wqjrxqbc.fsf@bsb.me.uk> |
| In reply to | #12019 |
Jerry Stuckle <jstucklex@attglobal.net> writes: > On 11/29/2013 12:01 AM, Ben Bacarisse wrote: >> Jerry Stuckle <jstucklex@attglobal.net> writes: <snip> >>> 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. >> >> Are we looking at different code? I don't see anywhere where you do >> that. > > No, I'm not doing it here. But that doesn't mean it isn't > necessary. To what code were you referring to when you said "as i did"? > He wanted the IP address for a reason. Yes, and the original post showed what for. Of course he might really want to do something else entirely, but it's not possible get anywhere staring with that assumption. <snip> >> As I said, we should agree to disagree. My conclusion is the reverse of >> yours. > > Yes, and you won't give it up, will you? I suggested that we agree to disagree two posts ago, but you did not want to -- you explained, again, why your choice was better. I won't advocate for my point of view (you'll notice that I don't do so in this post) if you won't. <snip> -- Ben.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.php
csiph-web