Skip to content

Instantly share code, notes, and snippets.

@CodyKochmann
Created May 9, 2017 20:40
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 CodyKochmann/e20e653c98c72aa039b84b447a1fffdd to your computer and use it in GitHub Desktop.
Save CodyKochmann/e20e653c98c72aa039b84b447a1fffdd to your computer and use it in GitHub Desktop.
use this to extract ip addresses from strings and arrays in php.
<?
class IP
{
# by: Cody Kochmann
# returns true if the input is a valid ip address
public static function validate($s)
{
# returns true if the $s is valid ip address
return (int)(!filter_var($s, FILTER_VALIDATE_IP) === false);
}
# extracts potential ips from a string into an array
public static function extract_legal_chars($s)
{
preg_match_all(
'/([a-fA-F0-9\.\:])+/',
$s,
$strings_with_legal_chars
);
return $strings_with_legal_chars[0];
}
# returns an array of valid IP addresses extracted from
# a string or array of strings
public function extract($s)
{
try {
$targets = IP::extract_legal_chars(is_array($s) ? implode(' ', $s) : $s);
$output = array();
foreach ($targets as $key => $value) {
if (IP::validate($value)) {
array_push($output, $value);
}
}
$output = array_unique($output);
return $output;
} catch (Exception $e) {
$this->log_error($e);
}
}
}
@mithgh20
Copy link

mithgh20 commented Aug 7, 2023

Login Successfull from IP - 103.79.249.94,Exchange-, NSE-FO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment