fork download
  1. <?php
  2.  
  3. $regex_ipv4 = "((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?!\\d)";
  4. $regex_integer = "\\d+";
  5.  
  6. $x = "123.123.123.123";
  7. $x2 = $x . ":12345";
  8.  
  9. preg_match("/^(?<ipv4>".$regex_ipv4.")(?::(?<port>".$regex_integer."))?$/", $x, $matches);
  10. preg_match("/^(?<ipv4>".$regex_ipv4.")(?::(?<port>".$regex_integer."))?$/", $x2, $matches2);
  11.  
  12. print_r($matches);
  13. print_r($matches2);
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 123.123.123.123
    [ipv4] => 123.123.123.123
    [1] => 123.123.123.123
    [2] => 123.123.123.123
)
Array
(
    [0] => 123.123.123.123:12345
    [ipv4] => 123.123.123.123
    [1] => 123.123.123.123
    [2] => 123.123.123.123
    [port] => 12345
    [3] => 12345
)