![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| On Fri, 15 Aug 2003 01:29:26 -0400 in <message-id:cd__a.739$3M4.380@lakeread04> "Phil Powell" <soazine@erols.com> wrote: > I have written a function that is supposed to compare the first three > nodes of an IP address, but it is riddled with errors that I have > given up trying to figure out. > > Is there an existing function out there that can do this? I want to > compare"127.0.0.1" with "127.0.0.100" and come with as a "match", or > "127.0.0.1" with "127.0.0.1", but no "match if "127.0.0.1" and > "127.255.0.0". > > Here is what I have: [ snip self-confessed non-working code ] Phil, Have a try with: <?php $ip1 = '127.0.0.1'; $ip2 = '127.0.0.100'; $ip3 = '127.255.0.0'; function ipcmp($ip_1, $ip_2) { if (substr($ip_1, 0, strrpos($ip_1, '.')) == substr($ip_2, 0, strrpos($ip_2, '.')) ) { return 'Match'; } else { return 'No Match'; } } echo ipcmp($ip1, $ip2) . "<br />\n"; echo ipcmp($ip3, $ip2) . "<br />\n"; echo ipcmp($ip1, $ip3); ?> HTH =) Regards, Ian -- Ian.H [Design & Development] digiServ Network - Web solutions www.digiserv.net | irc.digiserv.net | forum.digiserv.net Programming, Web design, development & hosting. | |||
| Advertisements |
| | #2 | ||
| I plain give up. Your solution is so simple it's amazing and works, mine is so complicated and fails. Phil "Ian.H [dS]" <ian@WINDOZEdigiserv.net> wrote in message news:20030815070820.2dfa51f2.ian@WINDOZEdigiserv.n et... > On Fri, 15 Aug 2003 01:29:26 -0400 in > <message-id:cd__a.739$3M4.380@lakeread04> > "Phil Powell" <soazine@erols.com> wrote: > > > I have written a function that is supposed to compare the first three > > nodes of an IP address, but it is riddled with errors that I have > > given up trying to figure out. > > > > Is there an existing function out there that can do this? I want to > > compare"127.0.0.1" with "127.0.0.100" and come with as a "match", or > > "127.0.0.1" with "127.0.0.1", but no "match if "127.0.0.1" and > > "127.255.0.0". > > > > Here is what I have: > > > [ snip self-confessed non-working code ] > > > Phil, > > Have a try with: > > > <?php > $ip1 = '127.0.0.1'; > $ip2 = '127.0.0.100'; > $ip3 = '127.255.0.0'; > > > function ipcmp($ip_1, $ip_2) { > if (substr($ip_1, 0, strrpos($ip_1, '.')) == > substr($ip_2, 0, strrpos($ip_2, '.')) > ) { > return 'Match'; > } else { > return 'No Match'; > } > } > > echo ipcmp($ip1, $ip2) . "<br />\n"; > echo ipcmp($ip3, $ip2) . "<br />\n"; > echo ipcmp($ip1, $ip3); > ?> > > > HTH =) > > > > Regards, > > Ian > > -- > Ian.H [Design & Development] > digiServ Network - Web solutions > www.digiserv.net | irc.digiserv.net | forum.digiserv.net > Programming, Web design, development & hosting. | |||
| | #3 | ||
| On Fri, 15 Aug 2003 04:30:48 -0400 in <message-id:eT0%a.745$3M4.707@lakeread04> "Phil Powell" <soazine@erols.com> wrote: > I plain give up. Your solution is so simple it's amazing and works, > mine is so complicated and fails. > > Phil Don't give up Phil.. I been "messing around" with PHP for around 5 years now.. it's great fun =) IMO, you over-complicated your method by splitting the IP addresses into separate numerical blocks. If you _really_ need to do some checking on those values as INTs, then fair enough, if it's literally to compare the first X chars, I just thought it'd be easier to work with as a complete string. Glad it works.. and FWIW.. I've posted some pretty "silly" questions here before too.. sometimes, it's just a matter of not seeing the forest for the trees =) Regards, Ian -- Ian.H [Design & Development] digiServ Network - Web solutions www.digiserv.net | irc.digiserv.net | forum.digiserv.net Programming, Web design, development & hosting. | |||
| | #4 | ||
| > function ipcmp($ip_1, $ip_2) { > if (substr($ip_1, 0, strrpos($ip_1, '.')) == > substr($ip_2, 0, strrpos($ip_2, '.')) > ) { > return 'Match'; > } else { > return 'No Match'; > } > } Or a one-liner using the ternary operator, if you prefer, using Ian.H's example: function ipcmp($ip_1, $ip_2) { return ((substr($ip_1, 0, strrpos($ip_1, '.')) == substr($ip_2, 0, strrpos($ip_2, '.'))) ? true : false); } Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22 www.lucas-smith.co.uk Senior Computing Technician (Web Technician) Department of Geography, University of Cambridge (01223 3)33390 & Webmaster, SPRI Scott Polar Research Institute, University of Cambridge | |||
| | #5 | ||
| That's how I handled it, as a one-liner with the ternary operator! Phil "Martin Lucas-Smith" <mvl22@cam.ac.uk> wrote in message news ine.SOL.4.44.0308151320160.23887-100000@green.csi.cam.ac.uk...> > > > > function ipcmp($ip_1, $ip_2) { > > if (substr($ip_1, 0, strrpos($ip_1, '.')) == > > substr($ip_2, 0, strrpos($ip_2, '.')) > > ) { > > return 'Match'; > > } else { > > return 'No Match'; > > } > > } > > > Or a one-liner using the ternary operator, if you prefer, using Ian.H's > example: > > > function ipcmp($ip_1, $ip_2) { > return ((substr($ip_1, 0, strrpos($ip_1, '.')) == substr($ip_2, 0, strrpos($ip_2, '.'))) ? true : false); > } > > > > Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22 > www.lucas-smith.co.uk > > Senior Computing Technician (Web Technician) > Department of Geography, University of Cambridge (01223 3)33390 > > & Webmaster, SPRI > Scott Polar Research Institute, University of Cambridge > > > | |||
| | #6 | ||
| "Martin Lucas-Smith" <mvl22@cam.ac.uk> wrote: > > function ipcmp($ip_1, $ip_2) { > return ((substr($ip_1, 0, strrpos($ip_1, '.')) == > substr($ip_2, 0, strrpos($ip_2, '.'))) > ? true : false); > } Why even bother with the ternary operator? (boolean condition) ? true : false is exactly equivalent to: (boolean condition) Ray | |||
| Featured Websites | ||||
|
![]() |
| Tags: addresses, compare, function, write |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| logging ip addresses behind proxies | somaBoyMX | PHP | 1 | 07-01-2007 4:00 PM |
| IQue 3600 Addresses | Rick | GPS | 2 | 06-25-2007 11:56 PM |
| Interpreting Hawaii addresses? | Plug | GPS | 3 | 06-25-2007 11:48 PM |
| Garmin 176C locating addresses | Normand Slowskan | GPS | 1 | 06-12-2007 5:13 PM |
| compare .. | Piet | Database | 2 | 06-10-2007 12:22 AM |
| Featured Websites | ||||
|