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

Is there a PHP command to

P: n/a

Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of course

-jools-
Jan 7 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
jo***@by.com wrote:
Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of
course

-jools-
Did you wonder enough to type
php sort ip addresses
into google and then press I'm feeling lucky?

Obviously not!
Jan 7 '08 #2

P: n/a
On Mon, 07 Jan 2008 19:48:09 +0100, <jo***@by.comwrote:
>
Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of course
Convert them to a number (using usort())?
http://nl2.php.net/manual/en/function.ip2long.php
http://nl2.php.net/manual/en/function.long2ip.php
--
Rik Wasmus
Jan 7 '08 #3

P: n/a
..oO(jo***@by.com)
>Just wondering what the simplest way to sort IP addresses would be
You could convert them to a long int, then use normal numeric sorting.

Micha
Jan 7 '08 #4

P: n/a
In article <5u*************@mid.individual.net>,
pa**********@btinternet.com says...
jo***@by.com wrote:
Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of
course

-jools-

Did you wonder enough to type
php sort ip addresses
into google and then press I'm feeling lucky?

Obviously not!
All google sites are blocked in my firewall.
I did wonder why newsgroups exist.
I guess you could tell me if you tried?

-jools-
Jan 8 '08 #5

P: n/a
In article <op***************@metallium.lan>, lu************@hotmail.com
says...
On Mon, 07 Jan 2008 19:48:09 +0100, <jo***@by.comwrote:

Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of course

Convert them to a number (using usort())?
http://nl2.php.net/manual/en/function.ip2long.php
http://nl2.php.net/manual/en/function.long2ip.php
--
Rik Wasmus
Aha - I knew there had to be something that helped - thanks Rik.

-jools-
Jan 8 '08 #6

P: n/a
In article <v5********************************@4ax.com>, ne*****@gmx.de
says...
.oO(jo***@by.com)
Just wondering what the simplest way to sort IP addresses would be

You could convert them to a long int, then use normal numeric sorting.

Micha
Pretty much what Rik said - looks like the way to go - thanks Michael.

-jools-
Jan 8 '08 #7

P: n/a
..oO(jo***@by.com)
>All google sites are blocked in my firewall.
Why is that?

You could get many answers much faster if you would do a quick search on
Google or the Google Groups archives. Most questions were already asked
and answered many times before.
>I did wonder why newsgroups exist.
Google it. SCNR

Micha
Jan 8 '08 #8

P: n/a
On Tue, 08 Jan 2008 12:01:51 +0100, <jo***@by.comwrote:
In article <op***************@metallium.lan>, lu************@hotmail.com
says...
>On Mon, 07 Jan 2008 19:48:09 +0100, <jo***@by.comwrote:
>
Just wondering what the simplest way to sort IP addresses would be
Is there a good way to sort that keeps them in sequence
asort() sorts on the first character so that doesnt quite do it of
course

Convert them to a number (using usort())?
http://nl2.php.net/manual/en/function.ip2long.php
http://nl2.php.net/manual/en/function.long2ip.php
--
Rik Wasmus

Aha - I knew there had to be something that helped - thanks Rik.
Hmm, come to think of it...
Allthough not intended for this use, version_compare() would do the trick
without any problems I think. As the behavious of version_compare is
clearly defined, and suited for (proven to be valid) IP-addresses, I'd go
with that:
<?php
$array = array(
'122.23.45.53',
'12.23.46.53',
'12.23.45.54',
'12.23.45.53',
'12.23.'
'12.23.45.53');
usort($array,'version_compare');
print_r($array);
?>

... unless you also need the insurance the IP adress is valid/stuff like
256 is captured/converted, in which case ip2long is still better.
--
Rik Wasmus
Jan 8 '08 #9

P: n/a
In article <15********************************@4ax.com>, ne*****@gmx.de
says...
>
You could get many answers much faster if you would do a quick search on
Google or the Google Groups archives. Most questions were already asked
and answered many times before.
which part of the words "blocked" and "firewall" don't you understand?

And no - I seriously doubt I'd get the answers either faster or better on
google even if I could use it as trawling through 30,000 SEO pages of
vaguely on topic rubbish more interested in attracting my attention
than actually telling me anything useful tends to take time and doesnt
have the hands on experience of the users of this group. Nor can it
suggest the "best" method of doing things.

Even if I could use google a newsgroup is always the best place to look
for those and a number of other reasons.

-jools-

Jan 8 '08 #10

P: n/a
jo***@by.com wrote:
In article <15********************************@4ax.com>,
ne*****@gmx.de says...
>>
You could get many answers much faster if you would do a quick
search on Google or the Google Groups archives. Most questions were
already asked and answered many times before.

which part of the words "blocked" and "firewall" don't you understand?

And no - I seriously doubt I'd get the answers either faster or
better on google even if I could use it as trawling through 30,000
SEO pages of vaguely on topic rubbish more interested in attracting
my attention
You will note that when I post a hint about using Google, I usually refer to
the "I'm feeling lucky", button. This button takes you straight to the top
suggestion in the list. I can accept that if you can't find the answer on
the first Google page then maybe it is worth while giving up and asking. But
when the answer is on the first hit, all you do by asking here is have
others look at that article for you.

What a waste of time.
Jan 8 '08 #11

P: n/a
In article <op***************@metallium.lan>, lu************@hotmail.com
says...
Hmm, come to think of it...
Allthough not intended for this use, version_compare() would do the tric=
k =

without any problems I think. As the behavious of version_compare is =

clearly defined, and suited for (proven to be valid) IP-addresses, I'd g=
o =

with that:
<?php
$array =3D array(
'122.23.45.53',
'12.23.46.53',
'12.23.45.54',
'12.23.45.53',
'12.23.'
'12.23.45.53');
usort($array,'version_compare');
print_r($array);
?>

.. unless you also need the insurance the IP adress is valid/stuff like =
=

256 is captured/converted, in which case ip2long is still better.
I'll go take a look at that too. Cheers.
Validation isn't a problem - I use the Data Minion DSVE for all my
validations now - it makes life SO much easier! The forms tool even
writes the PHP code for you.
I'd give you the link for it but this isn't my own development machine (I
can't remember it - or google it from here sadly)

-jools-

Jan 8 '08 #12

P: n/a
..oO(jo***@by.com)
>In article <15********************************@4ax.com>, ne*****@gmx.de
says...
>>
You could get many answers much faster if you would do a quick search on
Google or the Google Groups archives. Most questions were already asked
and answered many times before.

which part of the words "blocked" and "firewall" don't you understand?
Which part of the word "Why" don't you understand?
>And no - I seriously doubt I'd get the answers either faster or better on
google even if I could use it as trawling through 30,000 SEO pages of
vaguely on topic rubbish more interested in attracting my attention
than actually telling me anything useful tends to take time and doesnt
have the hands on experience of the users of this group. Nor can it
suggest the "best" method of doing things.
I also mentioned Google Groups, a Usenet archive.

On <http://groups.google.com/group/comp.lang.php/topicsfor example you
can get all the knowledge and experience of this group's users from the
last couple of years.
>Even if I could use google a newsgroup is always the best place to look
for those and a number of other reasons.
The best is to do some research on your own first by looking through the
archives or some FAQ sites before annoying the newsgroup regulars and
wasting their time with asking the same questions over and over again.
This is not a help desk.

Micha
Jan 8 '08 #13

P: n/a
In article <m6********************************@4ax.com>, ne*****@gmx.de
says...
>
The best is to do some research on your own first by looking through the
archives or some FAQ sites before annoying the newsgroup regulars and
wasting their time with asking the same questions over and over again.
This is not a help desk.

Micha
So go through this newsgroup and list the dates this question was asked
.... "over and over" - I'll take just the last three -
If you cant do that stop wasting everyones time with your self
opinionated nonesense that has nothing to do with this newsgroup.

This is a newsgroup for questions about PHP. This IS a 'helpdesk'. That
is exactly what it is for.

if YOU cant answer the questions asked here just keep it shut.

-jools-
Jan 10 '08 #14

P: n/a
..oO(jo***@by.com)
>In article <m6********************************@4ax.com>, ne*****@gmx.de
says...
>>
The best is to do some research on your own first by looking through the
archives or some FAQ sites before annoying the newsgroup regulars and
wasting their time with asking the same questions over and over again.
This is not a help desk.

So go through this newsgroup and list the dates this question was asked
... "over and over" - I'll take just the last three -
This was not related to your question, but meant in general. Why do you
think many popular newsgroups maintain an FAQ? Just for fun?
>If you cant do that stop wasting everyones time with your self
opinionated nonesense that has nothing to do with this newsgroup.

This is a newsgroup for questions about PHP. This IS a 'helpdesk'. That
is exactly what it is for.
You get what you pay for. Oh, you didn't pay for this "help desk"?
>if YOU cant answer the questions asked here just keep it shut.
I already answered your question. For free, BTW! But I have no problem
with keeping my mouth shut if you should ever come along with another
problem. I couldn't care less.

Remember: It's you who wants something from us, not the other way round.

And you still didn't answer my question about why you filter Google,
'cause I don't see any reason for that. But why should I care at all ...

EOT
Micha
Jan 10 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.