PHP Code to pull IP:Port from string with characters after the port #

Friday, July 4, 2014

I need a little help with this one and my RegEx knowledge is a little lacking with this one.


I have a proxy list that I'm trying to parse and separate the IP and port number from the string.


The string being read looks like this.(example 1)



121.121.121.121:8081 2.103384 Китай high 05-07-2014 09:25:17


and sometimes look like (example 2)



222.222.222.222:8081


When I use this code.



preg_match_all('@[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}\.@',$ip,$results);
$output = (preg_split('/:/',$proxy));
$ip = $output['0'];
$port = $output['1'];


It works great when there is just a IP:Port like in example #2 but in example #1 its also grabbing everything past the space so the port number looks like "8081 2.103384 Китай high 05-07-2014 09:25:17"


Is there a regex pattern I can use to have it stop at a space if it see's one?







http://ift.tt/TJehBD