Raised This Month: $51 Target: $400
 12% 

How to detect numbers and letters in an string?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
_lol_
Senior Member
Join Date: Apr 2009
Old 06-30-2009 , 20:44   How to detect numbers and letters in an string?
Reply With Quote #1

i think i have not to much to say, i want to "read" and string, and if it has a letter, block it, or if it has more than 3 numbers, block it.how to do it?
__________________
_lol_ is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 06-30-2009 , 20:55   Re: How to detect numbers and letters in an string?
Reply With Quote #2

PHP Code:
/* Returns true when value is digit. */
native isdigit(ch);

/* Returns true when value is letter. */
native isalpha(ch); 
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
_lol_
Senior Member
Join Date: Apr 2009
Old 06-30-2009 , 21:20   Re: How to detect numbers and letters in an string?
Reply With Quote #3

yeah..buy how should i use it?
look:

PHP Code:
public check(id)
{
    new 
say[300]
    
read_args(saycharsmax(say))
    
    
// Preparing the string
    
remove_quotes(say), trim(say)
    
    
// Only pressed enter?
    
if(equal(say""))
    {
        
zp_colored_print(id"^x04[ZP] ERROR !!!")
        return 
PLUGIN_HANDLED
    
}
    
// Two words?
    
if(contain(say" ") != -1)
    {
        
zp_colored_print(id"^x04[ZP]")
        return 
PLUGIN_HANDLED
    
}
    
// Very high number ??
    
if (str_to_num(say) > 255)
    {
        
zp_colored_print(id"^x04[ZP] ERROR !!!^x01 ")
        return 
PLUGIN_HANDLED
        
    
}
    
// Yeah, Passed !!
    
numbering(idsay)
    
    return 
PLUGIN_CONTINUE

i want to add to that check if the string has not more than 3 numbers, and if it has or not any letters. could you do it?
__________________
_lol_ is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-30-2009 , 23:54   Re: How to detect numbers and letters in an string?
Reply With Quote #4

PHP Code:
CountLetters( const szString[] )
{
    new 
iCount;
    
    for ( new 
strlenszString ) ; i++ )
        if ( 
isalphaszString] ) )
            
iCount++;
    
    return 
iCount;
}

CountNumbers( const szString[] )
{
    new 
iCount;
    
    for ( new 
strlenszString ) ; i++ )
        if ( 
isdigitszString] ) )
            
iCount++;
    
    return 
iCount;

__________________
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 07-01-2009 , 03:39   Re: How to detect numbers and letters in an string?
Reply With Quote #5

Don't use strlen() in a loop Bugsy :p.


Try this if String was not modified before:

Code:
CountLetters( const String[], const Len = sizeof( String ) ) {     new Count;         for ( new i = 0 ; i < Len ; i++ )     {         if ( isalpha( String[ i ] ) )  { Count++; }     }         return Count; } CountNumbers( const String[], const Len = sizeof( String ) ) {     new Count;         for ( new i = 0 ; i < Len; i++ )     {         if ( isdigit( String[ i ] ) )  { Count++; }     }         return Count; }

Otherwise use this :

Code:
CountLetters( const String[] ) {     new Count;     new Len = strlen( String );         for ( new i = 0 ; i < Len ; i++ )     {         if ( isalpha( String[ i ] ) )  { Count++; }     }         return Count; } CountNumbers( const String[] ) {     new Count;     new Len = strlen( String );         for ( new i = 0 ; i < Len; i++ )     {         if ( isdigit( String[ i ] ) )  { Count++; }     }         return Count; }
__________________

Last edited by Arkshine; 07-01-2009 at 12:55.
Arkshine is offline
_lol_
Senior Member
Join Date: Apr 2009
Old 07-01-2009 , 10:40   Re: How to detect numbers and letters in an string?
Reply With Quote #6

yeah ! thanks !! and...is there a way to detect signs?(i mean !"·$%&.,/() etc.)
__________________
_lol_ is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-01-2009 , 10:41   Re: How to detect numbers and letters in an string?
Reply With Quote #7

Quote:
Originally Posted by arkshine View Post
Don't use strlen() in a loop Bugsy :p.
My mistake

You left out a closing brace for the for-loop in both CountNumbers functions
__________________

Last edited by Bugsy; 07-01-2009 at 10:47.
Bugsy is offline
_lol_
Senior Member
Join Date: Apr 2009
Old 07-01-2009 , 10:56   Re: How to detect numbers and letters in an string?
Reply With Quote #8

PHP Code:
// Letters?
    
if (Countletters(saycharsmax(say) >= 1))
    {
        
zp_colored_print(id"^x04[Positive ZP6] ERROR !!!^x01 only numbers. Try it again")
        return 
PLUGIN_HANDLED
    

yeah, but if the string is "1dd" it pass, why?lol xd
__________________
_lol_ is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 07-01-2009 , 11:11   Re: How to detect numbers and letters in an string?
Reply With Quote #9

Quote:
// Letters?
if (Countletters(say, charsmax(say) >= 1))
{
zp_colored_print(id, "^x04[Positive ZP6] ERROR !!!^x01 only numbers. Try it again")
return PLUGIN_HANDLED
}
It will count letters, not detect if string is containing only chars.
TheRadiance is offline
Send a message via ICQ to TheRadiance
_lol_
Senior Member
Join Date: Apr 2009
Old 07-01-2009 , 11:25   Re: How to detect numbers and letters in an string?
Reply With Quote #10

so what sould i do?T_T
__________________
_lol_ is offline
Reply



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 15:11.


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