Wildcard and other search inputs

Discussion in 'PHP' started by guruguy, Jan 6, 2008.

  1. #1
    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.
     
    guruguy, Jan 6, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    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:
     
    Kaizoku, Jan 6, 2008 IP
    guruguy likes this.
  3. guruguy

    guruguy Active Member

    Messages:
    553
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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.
     
    guruguy, Jan 6, 2008 IP