String handling - how to separate decimal number of text in the string?

Asked

Viewed 73 times

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]

  • @Paulovictor would not be explode?

  • Yes, the correct indeed is explode, would be $time = explode("=", $tempo);, sorry, did running.

  • It had already done so. It continues giving NULL

  • 1

    Can show $strVetor result?

  • Try $time = explode("=", $tempo[7]);

  • 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" }

  • 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, set an example of input and one of the expected output... it will be easier to help you.

Show 4 more comments

2 answers

0

Just use the function explodes as below. Note that an array has been returned, so you know the position you need to recover. I hope I’ve helped.

<?php
$host = '192.168.0.24';
$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);
  for($i=0; $i<sizeof($strVetor); $i++){
     if(preg_match("/time/", $strVetor[$i])){
        $tempo = explode("time=", $strVetor[6]);
        echo "\n";
        echo "Tempo: " . $tempo[1];
        echo "\n";
        echo " ";
     }
  }
}
?>

Upshot:

root@79bbb258c614:/var/www/teste# php Lala.php

Time: 0.869
root@79bbb258c614:/var/www/test#

There may be a difference in the index of your array, depending on the version of your ping. Then, test the index and you can find it right. give a var_dump on $strVetor and see what index you need.

  • It didn’t work. I’m running debian

  • I performed the test before and it came out right... Can send the error that gave?

  • <?php&#xA;$host = '192.168.0.24';&#xA;$execPing = shell_exec('ping -c 1 ' . $host);&#xA;$vetExpl = explode("\n", $execPing);&#xA;$locStr = array_keys(preg_grep('/icmp_seq/', $vetExpl))[0];&#xA;$dadosVet = array_slice($vetExpl, $locStr, -5);&#xA;foreach ($dadosVet as $vetLinha) { $strVetor = explode(' ', $vetLinha); $tempo = explode("time=", $strVetor[6]); echo "<pre>"; echo " n"; echo "Time: ". $time[1]; echo " n"; echo " "; } ? > Result: root@79bbb258c614:/var/www/test# php Lala.php <pre> Time: 1.03

  • here appears NULL

  • So, do as I commented there in the reply (I edited it). Var_dump $strVetor and see what index you have to use.

  • it changes as you change the IP address you use use 192.168.0.1 or 10.117.10.100 is different than using localhost or site address

  • Dude, it’s simple.. Just test yourself on the current index, there’s time, and print :)

  • I edited again with the adjustment you need.

Show 4 more comments

0

That’s how it works

<?php
$host = 'localhost';
$execPing = shell_exec('ping -c 1 ' . $host);
$vetExpl = explode("\n", $execPing);
$locStr = array_keys(preg_grep('/time=/', $vetExpl))[0];
$dadosVet = array_slice($vetExpl, $locStr, -5);
foreach ($dadosVet as $vetLinha) {
  $strVetor = explode(' ', $vetLinha);
  $tempo = preg_grep('/time=/', $strVetor);
  $str = implode('',$tempo);
  $taxa = substr($str, 5);
}
echo "<pre>";
print_r($tempo);
print_r($taxa);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.