1
The algorithm below a ping on the host and does all the processing until you get the specific part I want that is the ping rate value
<?php
$host = 'www.google.com';
$execPing = shell_exec('ping -c 1 ' . $host);
$vetExpl = explode("\n", $execPing);
$locStr = array_keys(preg_grep('/icmp_seq/', $vetExpl))[0];
$dadosVet = array_slice($vetExpl, $locStr, -5);
foreach ($dadosVet as $vetLinha) {
$strVetor = explode(' ', $vetLinha);
$tempo = preg_grep('/time=/', $strVetor);
echo "<pre>";
var_dump($tempo);
}
?>
What happens is that the variable $tempo
, which contains the value of the fee, prints part text and part number. ex below:
array(1) {
[7]=>
string(9) "time=69.7"
}
What I want is to take only the numbers with the decimal, in this case the 69.7
I have tested with several examples and I cannot separate the decimal number from the text
I cannot use Indice, the position changes as the IP address changes using the address www google com br, it brings me the rate at position 7 using address inside the network, it brings me the rate at position 6
If all strings have this format, you can make 1 implode, it would be
$time = implode("=", $tempo);
and to access would$time[1]
– Paulo Victor
@Paulovictor would not be
explode
?– Sam
Yes, the correct indeed is explode, would be
$time = explode("=", $tempo);
, sorry, did running.– Paulo Victor
It had already done so. It continues giving NULL
– Andrei
Can show $strVetor result?
– Paulo Victor
Try
$time = explode("=", $tempo[7]);
– Paulo Victor
array(9) { [0]=> string(2) "64" [1]=> string(5) "bytes" [2]=> string(4) "from" [3]= string(24) "eze03s06-in-F3.1e100.net" [4]=> string(15) "(172.217.29.3):" [5]=> string(10) "icmp_seq=1" [6]=> string(6) "ttl=39" [7]=> string(9) "time=63.0" [8]=> string(2) "ms" }
– Andrei
with "$time = explode("=", $tempo[7]);" also does not work, because depending on the IP number you can change the position of the string in the array exe: if you use localhost, the position in the array becomes 6
– Andrei
Andrei, set an example of input and one of the expected output... it will be easier to help you.
– fernandosavio