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 #183943 is a reply to message #183939] Thu, 28 November 2013 12:27 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:
<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.
[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 07:38:48 GMT 2024

Total time taken to generate the page: 0.04153 seconds