首页 > 解决方案 > 如何拯救php中的两个分隔符?

问题描述

在此处输入图像描述

你好,我想将 ip 保存在一个变量中,我是通过控制台 ping 来实现的。

并标记我想要 [ 和 ] 中的内容。

$host = 'google.es';
function sacarIP($host)
{
    exec("ping -n 1 $host", $output, $status);
    $ip = $output[1];
    return $ip;
}

echo sacarIP($host);

这让我回到:

Haciendo ping a google.es [142.250.184.3] con 32 bytes de datos:

标签: php

解决方案


尝试:

$host = 'google.es';
function sacarIP($host)
{
    exec("ping -n 1 $host", $output, $status);
    preg_match('/\[\K[\d\.]+/', $output[1], $match);
    $ip = $match[0];
    return $ip;
}

echo sacarIP($host);

推荐阅读