AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How To check if string is the IP adress? (https://forums.alliedmods.net/showthread.php?t=85430)

GwynBleidD 02-10-2009 10:50

How To check if string is the IP adress?
 
I have problem how to check that entered string is the IP adress, anyone have solutions?

Arkshine 02-10-2009 11:32

Re: How To check if string is the IP adress?
 
You want to test if it's exactly and only a valid IP (0 .. 255 ) ? Or just if IP is containing the string.

Bugsy 02-10-2009 11:51

Re: How To check if string is the IP adress?
 
Explode the string using '.' as the delimiter. Then, verify you have 4 numerical elements and each one is >= 0 and <= 255.

Arkshine 02-10-2009 12:03

Re: How To check if string is the IP adress?
 
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.

AntiBots 02-10-2009 12:07

Re: How To check if string is the IP adress?
 
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)



Arkshine 02-10-2009 12:10

Re: How To check if string is the IP adress?
 
what ?

AntiBots 02-10-2009 12:12

Re: How To check if string is the IP adress?
 
Quote:

Originally Posted by arkshine (Post 759397)
what ?

To get a string of a specific octet ?

EDIT: Sorry in english is octet

Exolent[jNr] 02-10-2009 12:22

Re: How To check if string is the IP adress?
 
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); }

Arkshine 02-10-2009 12:34

Re: How To check if string is the IP adress?
 
Bad way of checking. Just because there are few '.' doesn't mean that it's an IP. :p

Exolent[jNr] 02-10-2009 12:39

Re: How To check if string is the IP adress?
 
I thought an IP must have 4 numbers?


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

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