PHP How to intercept this string?

I have a long list of IP addresses and ports. I'm trying to fit a port number from a list that looks something like 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 didn't have any problems with global IP using:

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

      

This works great for me, but I can't figure out how to preg map to the port that is at the end of the line.

+3


source to share


2 answers


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 can get a slightly smarter way of IP detection, but if your samples are well formed, that should be more than enough.

http://3v4l.org/FpGFD

+4


source


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

+5


source







All Articles