I am running a match with the preg_match function, and I need to search for an ip address and port in a string. Can someone give a search pattern that will return all traces of an ip and port in the ip : port format. i.e. xxx.xxx.xxx.xxx:yyyyy or xx.x.x.x:yy etc. Also could someone link to a resource that lists all the search operators (i.e. wildcards etc.) and what they do.
This is on the top of my head, not tested. $string = "192.268.0.1:8080"; preg_match("/([0-9]{2,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):([0-9]{2,4})/", $string, $x); list($string, $ip, $port) = $x; echo $ip; // 192.268.0.1 echo $port; // 8080 PHP: Why bother with that? Here is more efficient way. $string = "192.268.0.1:8080"; list($ip, $port) = explode(":", $string); echo $ip; // 192.268.0.1 echo $port; // 8080 PHP:
Thanks a lot for that, I've repped you. The first example looks like what I need but I am a bit too tired to test it, so I'll have another look tomorrow. The purpose of it is to grab the entire proxy and port from another web page. I already have the code to manipulate it, however I just need to know how to extract it from a much larger string, which I thing the first preg_match search pattern does.