Skip to content

Instantly share code, notes, and snippets.

@simbalinux
Created February 22, 2018 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simbalinux/a182446b29973d1e397f424682497de6 to your computer and use it in GitHub Desktop.
Save simbalinux/a182446b29973d1e397f424682497de6 to your computer and use it in GitHub Desktop.
search for host in string PHP
<?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