Created
May 9, 2017 20:40
-
-
Save CodyKochmann/e20e653c98c72aa039b84b447a1fffdd to your computer and use it in GitHub Desktop.
use this to extract ip addresses from strings and arrays in php.
This file contains 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
<? | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Login Successfull from IP - 103.79.249.94,Exchange-, NSE-FO