0

enter image description here

hello, I want to save the ip in a variable, I have occurred to me by pinging with console.

And mark that I want the content that is in [ and ].

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

echo sacarIP($host);

this brings me back to :

Haciendo ping a google.es [142.250.184.3] con 32 bytes de datos:
David S.M
  • 3
  • 1

1 Answers1

0

Try:

$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);
muesli
  • 106
  • 10