PHP Get value of a sentence inside the array

-2

I have a code below that checks the ping in MS of a url or IP, but what I need is to get the result's average value inside the array [10]

Example:

  

Mdia = 74ms

I just wanted the value 74 in a echo .

Below is a test code

<?php
function pingAddress($ip) {
    $pingresult = exec("ping  -n 3 $ip", $outcome, $status);
    if (0 == $status) {
        $status = "alive => ( ".print_r($outcome[10])." )";
    } else {
        $status = "fora";
    }
}
pingAddress("www.google.com");

?>
    
asked by anonymous 03.10.2018 / 04:00

1 answer

-1

You can do a runtime calculation using microtime()

<?php
function ping($host, $port = 80, $timeout = 10) { 
  $tB = microtime(true); // inicia um valor de timestamp 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); // checagem da url pretendida na porta desejada por um tempo determinado
  if (!$fP) { return "down"; } 
  $tA = microtime(true); // inicia um segundo valor de timestamp 
  return round((($tA - $tB) * 1000), 0)." ms"; // calcula o tempo de execução 
}

//Mostrando o ping caso esteja executando se não exibe 'down'
echo ping("www.google.com", 80, 10); 
    
03.10.2018 / 04:26