By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,050 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,050 IT Pros & Developers. It's quick & easy.

Best way to pull an IP out of string?

P: n/a
I've been using two instances of substr to pull an IP address out of a
string. I know there's a more efficient way. Anyone? The string is
like this:

HOST 61.2.135.45 blocked.

Richard

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
I've been using two instances of substr to pull an IP address out of a
string. I know there's a more efficient way. Anyone? The string is
like this:

HOST 61.2.135.45 blocked.

Richard

preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
$ip = $matches[0];
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
I've been using two instances of substr to pull an IP address out of a
string. I know there's a more efficient way. Anyone? The string is
like this:

HOST 61.2.135.45 blocked.

Richard

preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
$ip = $matches[0];
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


The only correct way is to pull the numbers out with an expression and test
them for range validity manually. This works:

function testipformatvalid($input) {
preg_match_all('/\d{1,3}/',$input,$r);
if(count($r[0]) != 4) return false;
foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
return true;
}

Garp
Jul 17 '05 #3

P: n/a
On Sat, 10 Apr 2004 09:49:29 +0000, Garp wrote:

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
> I've been using two instances of substr to pull an IP address out of a
> string. I know there's a more efficient way. Anyone? The string is
> like this:
>
> HOST 61.2.135.45 blocked.

preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
$ip = $matches[0];

The only correct way is to pull the numbers out with an expression and test
them for range validity manually. This works:

function testipformatvalid($input) {
preg_match_all('/\d{1,3}/',$input,$r);
if(count($r[0]) != 4) return false;
foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
return true;
}

But that wasn't part of the question ;)

From the OP.. it would appear this would be from a log file of some kind
inwhich you can almost guarantee that the IP address will be a valid IP
address and not need to have each of its octects parsed.

Also, please check your function.. I fail to see where it actually pulls
the IP address out (ie: as I had it stored in $ip) which appears to be
what the OP was asking for.. not to validate each octet.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Sat, 10 Apr 2004 09:49:29 +0000, Garp wrote:

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:

> I've been using two instances of substr to pull an IP address out of a > string. I know there's a more efficient way. Anyone? The string is
> like this:
>
> HOST 61.2.135.45 blocked.
preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
$ip = $matches[0];
The only correct way is to pull the numbers out with an expression and

test them for range validity manually. This works:

function testipformatvalid($input) {
preg_match_all('/\d{1,3}/',$input,$r);
if(count($r[0]) != 4) return false;
foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
return true;
}

But that wasn't part of the question ;)

From the OP.. it would appear this would be from a log file of some kind
inwhich you can almost guarantee that the IP address will be a valid IP
address and not need to have each of its octects parsed.

Also, please check your function.. I fail to see where it actually pulls
the IP address out (ie: as I had it stored in $ip) which appears to be
what the OP was asking for.. not to validate each octet.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


You already answered his question, I was providing extra services. 8) I did
typo tho:
The only correct way is to pull the numbers out ...
should be
The only correct way to validate the address is to pull the numbers out
....

Garp
Jul 17 '05 #5

P: n/a
On Sat, 10 Apr 2004 10:26:50 +0000, Garp wrote:
[ snip ]

Also, please check your function.. I fail to see where it actually pulls
the IP address out (ie: as I had it stored in $ip) which appears to be
what the OP was asking for.. not to validate each octet.


You already answered his question, I was providing extra services. 8) I did
typo tho:
The only correct way is to pull the numbers out ...
should be
The only correct way to validate the address is to pull the numbers out

Fair play.. makes more sense now with the typo "correction" =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #6

P: n/a

"Garp" <ga***@no7.blueyonder.co.uk> wrote in message
news:Jq*******************@news-text.cableinet.net...
The only correct way is to pull the numbers out with an expression and test them for range validity manually. This works:

function testipformatvalid($input) {
preg_match_all('/\d{1,3}/',$input,$r);
if(count($r[0]) != 4) return false;
foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
return true;
}


Why roll your own function when there's a built-in one?

if(ip2long($address) < 0) {
echo "Invalid IP address!";
}
Jul 17 '05 #7

P: n/a
On Sat, 10 Apr 2004 10:31:00 GMT, "Ian.H" <ia*@WINDOZEdigiserv.net>
wrote:
On Sat, 10 Apr 2004 10:26:50 +0000, Garp wrote:
[ snip ]

Also, please check your function.. I fail to see where it actually pulls
the IP address out (ie: as I had it stored in $ip) which appears to be
what the OP was asking for.. not to validate each octet.



You already answered his question, I was providing extra services. 8) I did
typo tho:
The only correct way is to pull the numbers out ...
should be
The only correct way to validate the address is to pull the numbers out

Fair play.. makes more sense now with the typo "correction" =)

Regards,

Ian

Thanks to both of you!

Richard
Jul 17 '05 #8

P: n/a
In article <u4********************************@4ax.com>,
Richard T. Cunningham <no****@nospam.com> wrote:
I've been using two instances of substr to pull an IP address out of a
string. I know there's a more efficient way. Anyone? The string is
like this:

HOST 61.2.135.45 blocked.

Richard


As Ian said, or you could us a general preg_match for ip's like so:

preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $string, $matches)

That will put any occurance of a ip number (x.x.x.x, xx.xx.xx.xx,
xxx.xxx.xxx.xxx or any mix) in $matches (even invalid ones, such as
666.666.666.666).

--
Sandman[.net]
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.