0

I'm trying to search the array $outcome for the string "Destination host unreachable" and it doesn't work.

I'm not sure what I'm doing wrong and was wondering if someone could lend a hand?

   $ip = 192.168.1.30

   $pingresult = exec("ping -n 1 $ip", $outcome, $status);

   if ($status == 0) 
       {                                        
           if(in_array("Destination host unreachable", $outcome))
               {
                   echo "Unreachable";
               }
           else
               {
                   echo "Alive";
               }   
       }

P.S. Please note I'm on a Windows machine so the exit status is different to Linux etc.

2 Answers 2

1

you can also try.

if(array_search($outcome, "Destination host unreachable") !== false){
 echo 'not reachable';
}
2
  • Thanks, using this. Although I do get 'expects a string, not an array' error. Nevertheless it seems to be working like that. Commented Feb 10, 2012 at 20:16
  • ohh ok. this function is for string thats why notworking for array. try this it will work. if(array_search($outcome, "Destination host unreachable") !== false){ echo 'not reachable'; } Commented Feb 11, 2012 at 4:58
1

in_array returns true on exact matches only. So, if there are additional characters on the line (including white space), it won't match. You may wish to loop through the array using preg_match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.