user2129623
user2129623

Reputation:

Understanding preg_match formula

I anyhow got my stuff working with following line, but I really could not understand.

if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Desktop/test/b '$test'"),$matches))
                {
                        $x = (int) $matches[1][0]; //optionally cast to int
                        $y = (int) $matches[1][1];
                        $pcount= round((100*$x)/($x+$y),2);
                        $ncount= round((100*$y)/($x+$y),2);
                }

b is executable file, which gives result something like x=10 and y=20

Can some one explain me whatever inside if()

Upvotes: 3

Views: 636

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

Pattern details:

[^=]*       # is a negated character class that means "all characters except ="
            # * is quantifier that means zero or more times
            # note that it seems more logical to replace it with + 
            # that means 1 or more times

=           # literal =

(           # open the capturing group 1
  [^;@]*    # all characters except ";" and "@", zero or more times
            # (same notice) 
)           # close the capturing group 1

Upvotes: 2

Lajos Veres
Lajos Veres

Reputation: 13725

This: /[^=]*=([^;@]*)/ collects all ...=... things to the $matches array.

  • [^=] means any character except =
  • [^;@] means any character except ; and @
  • () means collect it into $matches explicitly

The $pcount/$ncount makes percent from the values showing theirs ratio.

Upvotes: 2

Related Questions