Php extract ip from string

I'm trying to use a regex to capture ip addresses in lines of text. Right now it's kind of screwed up. My code returns ip addresses when lines of text don't contain ip addresses and sometimes it returns "Script)". What am I doing wrong? I would like to return the ip addresses of lines that actually have them, and return nothing if they don't.

Text

2014-06-02 11:53:54.410 -0700   Information 638 NICOLE  Client "123456" opening a connection from "123456.local (207.230.229.204)" using "Go 13.0.4 [fmapp]".
2014-06-02 11:54:52.504 -0700   Information 98  NICOLE  Client "123456 (123456.local) [207.230.229.204]" closing database "FMServer_Sample" as "Admin".
2014-06-02 12:07:33.433 -0700   Information 638 NICOLE  Client "[WebDirect]" opening a connection from "207.230.229.204 (207.230.229.204)" using "Win Chrome 35.0 [fmwebdirect]".
2014-06-02 13:05:00.088 -0700   Information 638 NICOLE  Client "Showare Update" opening a connection from "FileMaker Script" using "Server 13.0v1 [fmapp]".
2014-06-02 13:05:22.366 -0700   Information 98  NICOLE  Client "Showare Update (FileMaker Script)" closing database "cac" as "opus".
2014-06-02 12:08:04.165 -0700   Information 98  NICOLE  Client "[WebDirect] (207.230.229.204) [207.230.229.204]" closing database "FMServer_Sample" as "Admin".

PHP

if(preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $line, $ip_matches)) {
    $client_ips = $ip_matches[1];
}

print "<pre>;
print_r($client_ips);
print "</pre>";

Output

207.230.229.204
207.230.229.204
207.230.229.204
207.230.229.204
207.230.229.204
Script)

asked Jun 9, 2014 at 2:14

Php extract ip from string

3

Consider removing the beginning of string ^ and end of string $ anchors seeing how your IP addresses are not at the beginning/end of the string in your provided text.

if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $line, $ip_match)) {
   echo $ip_match[0];
}

If you want all IP addresses, use preg_match_all()

preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $line, $ip_matches);
print_r($ip_matches[0]);

answered Jun 9, 2014 at 2:32

Php extract ip from string

hwndhwnd

68.5k4 gold badges91 silver badges126 bronze badges

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);
}
}
}

How can I get IP address in PHP?

Using getenv() function: To get the IP Address,we use getenv(“REMOTE_ADDR”) command. The getenv() function in PHP is used for retrieval of values of an environment variable in PHP. It is used to return the value of a specific environment variable.

How do I extract an IP address from a URL?

In Windows, you can find the IP address of a website using tracert command..
At the prompt, type in tracert and leave a single space, then type in your website's address (excluding the “www.” part)..
For example- tracert www.serverguy.com..
Press Enter..

How do I find my client IP?

Getting the Client IP Address.
Use the system environment variable $_SERVER["REMOTE_ADDR"] . One benefit is that on Pantheon this takes into account if the X-Forwarded-For header is sent in cases when a request is filtered by a proxy..
Use Drupal's ip_address() function..

Which code will return the IP address of the client?

The answer is to use $_SERVER variable. For example, $_SERVER["REMOTE_ADDR"] would return the client's IP address.