联合 IP 地址和端口 (正则表达式)


Union ip address and port (regexp)

我有一行字符串2h 2m 202.29.216.236 3128 flag Thailand

$string = preg_replace("/([0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3})|('s+'d{2,4}'s+)/", "$1:$2", $string);

我需要像这样获取 IP 地址:202.29.216.236:3128。

但是我的代码是这样做的:109.197.92.60:: 8080.

我不明白空格键和两个冒号在哪里。

提前感谢,对不起我的英语不好。

你为什么不使用explode()

<?php
$str="2h 2m 202.29.216.236 3128 flag Thailand";
$str=explode(' ',$str);
echo $str[2].":".$str[3];//202.29.216.236:3128

尝试

$string = preg_replace("/([0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3})'s+('d{2,4})/", "$1:$2", $string);
空格来自第二个

参数内的 ''s+,第二个冒号来自 | char。