473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Best way to pull an IP out of string?

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
8 4780
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

"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
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

"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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mike | last post by:
I need to be able to pull the current <Title> tag from the header into the body of the html page. We started coding this with php, and we were able to get it working. However the entire page is...
4
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
4
by: Dave | last post by:
I am saving a class by giving it the "Serializable" attribute, and calling Serialize. Is there a way that I can pull the value of an individual member of the class, out of the resulting xml file,...
7
by: Ali-R | last post by:
Hi all, I am getting a CSV file like this from our client: "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate **each filed value** against a set of rules ,for...
1
by: richardkreidl | last post by:
Here is my Combobx code: Private Sub cboTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTo.SelectedIndexChanged Dim Target As String Dim Value As...
12
by: MrQuan | last post by:
G'day all, I have a requirement to communicate between two or more PCs over the Internet, however I have no idea how to go about this. I'm not talking about a chat programme as such, I want to...
4
by: ink | last post by:
Hi all, I am trying to pull some financial data off of an HTML web page so that I can store it in a Database for Sorting and filtering. I have been thinking about this for some time and trying...
7
by: Brian | last post by:
I am looking for a way to store maybe in an array or dictionary... two fields.... first field would be a string... second would be a date... for example.. if i called up myinfo(0) it would show...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.