Results 1 to 9 of 9

Thread: Alphanumeric only

  1. #1

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734

    Alphanumeric only

    How can I make it so only alphanumeric characters can be used?

  2. #2
    ricmitch_uk
    Guest
    There's no specific function in the PHP manual (I recommend anyone who uses PHP to download it in PDF format).

    That means you have to do it the hard way, and compare every character in the string to an array of characters you want to allow

  3. #3
    ricmitch_uk
    Guest
    This is the type of function you want to be creating:
    PHP Code:
    function alphanumeric($string) {
        
    $alphachars[0] = "9";
        
    $alphachars[1] = "a";
        
    $alphachars[2] = "b";
        
    $alphachars[3] = "c";
        
    $alphachars[4] = "d";
        
    $alphachars[5] = "e";
        
    $alphachars[6] = "f";
        
    $alphachars[7] = "g";
        
    $alphachars[8] = "h";
        
    $alphachars[9] = "i";
        
    $alphachars[10] = "j";
        
    $alphachars[11] = "k";
        
    $alphachars[12] = "l";
        
    $alphachars[13] = "m";
        
    $alphachars[14] = "n";
        
    $alphachars[15] = "o";
        
    $alphachars[16] = "p";
        
    $alphachars[17] = "q";
        
    $alphachars[18] = "r";
        
    $alphachars[19] = "s";
        
    $alphachars[20] = "t";
        
    $alphachars[21] = "u";
        
    $alphachars[22] = "v";
        
    $alphachars[23] = "w";
        
    $alphachars[24] = "x";
        
    $alphachars[25] = "y";
        
    $alphachars[26] = "z";
        
    $alphachars[27] = "0";
        
    $alphachars[28] = "1";
        
    $alphachars[29] = "2";
        
    $alphachars[30] = "3";
        
    $alphachars[31] = "4";
        
    $alphachars[32] = "5";
        
    $alphachars[33] = "6";
        
    $alphachars[34] = "7";
        
    $alphachars[35] = "8";
        for (
    $i=0;$i<strlen($string);$i++) {
            for (
    $j=0;$j<36;$j++) {
                if(
    strtolower(substr($string$i1)) == $alphachars[$j]) {
                    
    $charok == 1;
                }
            }
            if (!
    $charok) {
                return 
    "false";
            }
            
    $charok NULL;
        }
        return 
    "true";

    HTH

  4. #4

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Great thanks a bunch. Just wondering how you call a function on PHP?

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I would recommend a minor modifiction to ric's code, at the bottom take the quotes away from around the return values, e.g change
    PHP Code:
    return "false"
    to
    PHP Code:
    return false
    . Then you can just do this
    PHP Code:
    If (alphanumeric("something groovy")) {
    ....

    If you want to test for false, put an exclamation mark before "alphanumeric"

  6. #6
    ricmitch_uk
    Guest
    doh! I missed that?


    You just call it like chris said,
    PHP Code:
    if(alphanumeric($mystring)) {
         echo 
    "The phrase is alphanumeric";
    } else {
         echo 
    "The phrase is not alphanumeric";

    or if you for some reason wanted to store the outcome in a variable, you would use:
    PHP Code:
    $isalpha alphanumeric($mystring); 
    Using your own functions, is just like using the ones built into PHP and it's dll's. eg. mysql_connect

    HTH

  7. #7

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Thanks both of you

  8. #8
    ricmitch_uk
    Guest
    That's OK Just don't corrupt my avatar

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    already done mine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width