Raised This Month: $ Target: $400
 0% 

How To check if string is the IP adress?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GwynBleidD
Junior Member
Join Date: Feb 2009
Old 02-10-2009 , 10:50   How To check if string is the IP adress?
Reply With Quote #1

I have problem how to check that entered string is the IP adress, anyone have solutions?
GwynBleidD is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-10-2009 , 11:32   Re: How To check if string is the IP adress?
Reply With Quote #2

You want to test if it's exactly and only a valid IP (0 .. 255 ) ? Or just if IP is containing the string.
Arkshine is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-10-2009 , 11:51   Re: How To check if string is the IP adress?
Reply With Quote #3

Explode the string using '.' as the delimiter. Then, verify you have 4 numerical elements and each one is >= 0 and <= 255.
__________________

Last edited by Bugsy; 02-10-2009 at 11:54.
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-10-2009 , 12:03   Re: How To check if string is the IP adress?
Reply With Quote #4

Here a safe example using regex to check if the string is exactly a valid IP :

Code:
    #include <amxmodx>     #include <regex>         #pragma ctrlchar '#'         new const g_szPattern[] = "^\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$";     new Regex:g_hPreCompiledPattern;         public plugin_init ()     {         new Ret, szError[ 2 ];         g_hPreCompiledPattern = regex_compile( g_szPattern, Ret, szError, charsmax ( szError ) );     }     stock bool:IsValidIP ( const szSource[] )     {         new Ret;         return regex_match_c( szSource, g_hPreCompiledPattern, Ret ) > 0;     }

You have just to call IsValidIP() in your function :

Code:
if ( IsValidIP( "125.7.245.18" ) ) { }

As notes, I'm using regex_compile() because it's something you will probably check severals times so It's better to pre-compile the pattern one time. Also since the pattern use ^ which is the default control char in Pawn, it's necessary to change it by another character. I've choose # but you can change it.

Last edited by Arkshine; 02-10-2009 at 14:55.
Arkshine is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-10-2009 , 12:07   Re: How To check if string is the IP adress?
Reply With Quote #5

There is another way?, Sorry the off topic

PHP Code:
stock get_ip_octeros(const IP[], OCNUMOCTERO[], len)
{
        new 
searhpoint 1
        
new temp[3], count
        
for(new 016i++)
        {
                if( 
equal(IP[i], ".") )
                {
                        
searhpoint++
                        continue
                }
 
                if( 
searhpoint == OCNUM )
                {
                        
temp[count++] = IP[i]
                }                
        }
 
        return 
copy(OCTEROlentemp)

__________________

Last edited by AntiBots; 02-10-2009 at 12:12.
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-10-2009 , 12:10   Re: How To check if string is the IP adress?
Reply With Quote #6

what ?
Arkshine is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-10-2009 , 12:12   Re: How To check if string is the IP adress?
Reply With Quote #7

Quote:
Originally Posted by arkshine View Post
what ?
To get a string of a specific octet ?

EDIT: Sorry in english is octet
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-10-2009 , 12:22   Re: How To check if string is the IP adress?
Reply With Quote #8

I wrote this because it was easy:
Code:
bool:IsValidIP(const ip[]) {     static temp[64];     new len = copy(temp, sizeof(temp) - 1, ip);         new sections;     for( new i = 0; i < len; i++ )     {         switch( temp[i] )         {             case '.': sections++;             case ':': { }             case '0' .. '9': { }             default: return false;         }     }         return (sections == 3); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 02-10-2009 at 12:39.
Exolent[jNr] is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-10-2009 , 12:34   Re: How To check if string is the IP adress?
Reply With Quote #9

Bad way of checking. Just because there are few '.' doesn't mean that it's an IP. :p
Arkshine is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-10-2009 , 12:39   Re: How To check if string is the IP adress?
Reply With Quote #10

I thought an IP must have 4 numbers?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:04.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode