SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How to exclude some characters?

    I have a randomized code generating 5 characters (mixed numbers and letters).

    Thenmy users will use that code to login, but some numbers and letters are a little hard to identify in different fonts, so I would like to check if a character is in the code, otherwise generate a new code.

    Is there anyone who's got a clue on what to do?

    Let's say my generated code is called $code.

    So I thought there must be like an array checking from left to right maybe, but I'm really bad when it comes to arrays.
    It's probably very simple to exclude, let's say all zeros in $code and generate a new code if it finds a zero in the 5 digit code.

    Can anyone give me a hint here at least?

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it would be easier to just keep those characters from being put in in the first place...something like:
    PHP Code:
       function create_password($length 5) {
           
    $chars "0123456789abcdefghijklmnopqrstuvwxyz";
           
    $password "";
           for (
    $i 0$i $length$i++) {
               
    $char_pos floor(rand(0strlen($chars) - 1));
               
    $password .= substr($chars$char_pos1);
           }
           return 
    $password;
       } 
    then just add or remove characters from the $chars variable. or if you can post your random char generator someone could probably show you how to modify that.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That sounded like a good idea, to remove that charachters from the random code.
    But when looking at your code I think I understand how to do it and I will try that as well.
    It's good to do it the hard way to learn something I guess.

    ;-)


Bookmarks

Posting Permissions

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