我正在尝试从标准Oracle连接字符串中提取FQDN和端口号。 我已经想出了如何单独提取FQDN和PORT,但我正在寻找一个单独的php regexp,这将允许我同时提取所有FQDN和PORT。
示例连接字符串:
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN1)(PORT=1234))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN2)(PORT=5678))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN3)(PORT=9012))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN4)(PORT=3456)))(CONNECT_DATA=(SERVICE_NAME=STUFF)))
我的预期结果是一个数组:
[0][0] => FQDN1, [0][1] => 1234,
[1][0] => FQDN2, [1][1] => 5678,
[2][0] => FQDN3, [2][1] => 9012,
[3][0] => FQDN4, [3][1] => 3456
你们的任何帮助都将受到赞赏。
感谢。
答案 0 :(得分:2)
您可以像这样使用preg_match_all:
if (preg_match_all('~HOST=([^)\s]+).*?PORT=([^)\s]+)~', $str, $arr))
print_r($arr);
答案 1 :(得分:0)
$str="(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN1)(PORT=1234))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN2)(PORT=5678))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN3)(PORT=9012))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN4)(PORT=3456)))(CONNECT_DATA=(SERVICE_NAME=STUFF)))";
preg_match_all('/\(HOST=(FQDN\d)\)\(PORT=(\d+)\)/', $str, $matches, PREG_SET_ORDER);
foreach($matches as $k=>$match)
array_shift($matches[$k]);
使用PREG_SET_ORDER按照您希望的方式排列它们。然后是array_shift完全匹配。
只要有可能,您应该阅读文档:
答案 2 :(得分:0)
试试这个:
$subject = <<<LOD
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN1)(PORT=1234))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN2)(PORT=5678))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN3)(PORT=9012))(ADDRESS=(PROTOCOL=TCP)(HOST=FQDN4)(PORT=3456)))(CONNECT_DATA=(SERVICE_NAME=STUFF)))
LOD;
$pattern = '~\(HOST=([^)]+)\)\(PORT=(\d+)\)~';
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
// removes the whole match (index 0) for each result to only let the capture groups
foreach ($matches as &$match) {
array_shift($match);
}