Created
February 22, 2018 18:53
-
-
Save simbalinux/a182446b29973d1e397f424682497de6 to your computer and use it in GitHub Desktop.
search for host in string PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//simulating the array from string: | |
$s = ' | |
[2018-02-19 14:08:32] [info] status=GOOD&unit_status=GOOD&hostname=acme | |
[2018-02-19 14:08:32] [alert] Wrong status! (Not "DEGRADED") | |
[2018-02-19 14:08:37] [info] Request with params {"status":"GOOD","unit_status":"GOOD","hostname":"joe"} | |
[2018-02-19 14:08:37] [info] status=GOOD&unit_status=GOOD&hostname=joe | |
[2018-02-19 14:08:37] [alert] Wrong status! (Not "DEGRADED") | |
'; | |
$ar = explode("\n",$s); | |
$hostsToFind = [ | |
'acme'=>0, | |
'joe'=>0, | |
'bleh'=>0, | |
]; | |
foreach($ar as $inputRow){ | |
if (preg_match('/&hostname=(.+)$/', $inputRow, $token)) { | |
$host = $token[1]; | |
if (array_key_exists($host, $hostsToFind)) { | |
$hostsToFind[$host]++; | |
} | |
} | |
} | |
foreach($hostsToFind as $host => $found) { | |
if (!$found) { | |
echo "Host not found : ".$host."\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment