user1294097
user1294097

Reputation: 153

PHP How to preg match this string?

I have long list of IP addresses and ports. I'm trying to preg match the port number from a list that looks similar to this:

/connect=;;41.26.162.36;;192.168.0.100;;8081;;
/connect=;;98.250.16.76;;192.168.0.24;;8080;;
/connect=;;216.152.60.12;;192.168.1.103;;8090;;
/connect=;;91.11.65.110;;192.168.1.3;;8081;;

I have had no problems preg matching the global IP address using:

preg_match_all('/connect=;;(.+?);;/', $long, $ip);
            $ip = $ip[1][0];
            print_r($ip);

This works perfectly for me, but i cannot figure out how to preg match the port which sits at the end of the line.

Upvotes: 4

Views: 338

Answers (3)

mickmackusa
mickmackusa

Reputation: 47874

Since your logs will be strictly and reliably formatted, you might just scan the formatted string.

https://www.php.net/manual/en/function.sscanf.php

sscanf() doesn't produce the unneeded fullstring match like preg_match_all() does.

Code: (Demo)

$logs = <<<LOGS
/connect=;;41.26.162.36;;192.168.0.100;;8081;;
/connect=;;98.250.16.76;;192.168.0.24;;8080;;
/connect=;;216.152.60.12;;192.168.1.103;;8090;;
/connect=;;91.11.65.110;;192.168.1.3;;8081;;
LOGS;

foreach(explode(PHP_EOL, $logs) as $line) {
    sscanf($line, "/connect=;;%[^;];;%[^;];;%d;;", $ip1, $ip2, $port);
    var_export(
        [$ip1, $ip2, $port]
    );
    echo "\n";
}

Output:

array (
  0 => '41.26.162.36',
  1 => '192.168.0.100',
  2 => 8081,
)
array (
  0 => '98.250.16.76',
  1 => '192.168.0.24',
  2 => 8080,
)
array (
  0 => '216.152.60.12',
  1 => '192.168.1.103',
  2 => 8090,
)
array (
  0 => '91.11.65.110',
  1 => '192.168.1.3',
  2 => 8081,
)

Upvotes: 0

Sara
Sara

Reputation: 729

Just to expand on the above answer a bit:

if (preg_match_all('/connect=;;([\d\.]+);;([\d\.]+);;(\d+);;/',
                   $long, $matches, PREG_SET_ORDER)) {
  foreach($matches as $match) {
    list(,$ip1, $ip2, $port) = $match;
    /* Do stuff */
    echo "{$ip1} => {$ip2}:{$port}\n";
  }
}

You could get a bit more clever on the IP detection, but if your samples are well formed, this should be more than enough.

http://3v4l.org/FpGFD

Upvotes: 5

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Try this way ...

    $re = "/connect=;;(.+?);;(\d{4});;/";
    $str = "/connect=;;41.26.162.36;;192.168.0.100;;8081;;/connect=;;98.250.16.76;;
    192.168.0.24;;8080;;/connect=;;216.152.60.12;;192.168.1.103;;8090;;/connect=;;
    91.11.65.110;;192.168.1.3;;8081;;";

    preg_match_all($re, $str, $matches);

Explanation:

  /connect=;;(.+?);;(\d{4});;/g

   connect=;; matches the characters connect=;; literally (case sensitive)
  1st Capturing group (.+?)
    .+? matches any character (except newline)
        Quantifier: +? Between one and unlimited times, as few times as possible, 
   expanding as needed [lazy]
   ;; matches the characters ;; literally
  2nd Capturing group (\d{4})
    \d{4} match a digit [0-9]
        Quantifier: {4} Exactly 4 times
  ;; matches the characters ;; literally
  g modifier: global. All matches (don't return on first match)

enter image description here

Upvotes: 5

Related Questions