Another function, used in my Data Generator. This function accepts a single string as an argument, which may contain characters with pre-set meaning (see doc below). They are converted to their appropriate meaning and the entire string is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*------------------------------------------------------------------------*\
 Function: generate_random_alphanumeric_str
 Purpose:  converts the following characters in the string and returns it:
           C, c, A - any consonant (upper case, lower case, any)
           V, v, B - any vowel (upper case, lower case, any)
           L, l, V - any letter (upper case, lower case, any)
           X       - 1-9
           x       - 0-9
\*------------------------------------------------------------------------*/

function generate_random_alphanumeric_str($str)
{
  $letters    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $consonants = "BCDFGHJKLMNPQRSTVWXYZ";
  $vowels     = "AEIOU";

  $new_str = "";
  for ($i=0; $i<strlen($str); $i++)
  {
    switch ($str[$i])
    {
      // Numbers
      case "X": $new_str .= rand(1,9);  break;
      case "x": $new_str .= rand(0,9);  break;

      // Letters
      case "L": $new_str .= $letters[rand(0, strlen($letters)-1)]; break;
      case "l": $new_str .= strtolower($letters[rand(0, strlen($letters)-1)]); break;
      case "D":
        $bool = rand()&1;
        if ($bool)
          $new_str .= $letters[rand(0, strlen($letters)-1)];
        else
          $new_str .= strtolower($letters[rand(0, strlen($letters)-1)]);
        break;

      // Consonants
      case "C": $new_str .= $consonants[rand(0, strlen($consonants)-1)];      break;
      case "c": $new_str .= strtolower($consonants[rand(0, strlen($consonants)-1)]);  break;
      case "E":
        $bool = rand()&1;
        if ($bool)
          $new_str .= $consonants[rand(0, strlen($consonants)-1)];
        else
          $new_str .= strtolower($consonants[rand(0, strlen($consonants)-1)]);
        break;

      // Vowels
      case "V": $new_str .= $vowels[rand(0, strlen($vowels)-1)];  break;
      case "v": $new_str .= strtolower($vowels[rand(0, strlen($vowels)-1)]);  break;
      case "F":
        $bool = rand()&1;
        if ($bool)
          $new_str .= $vowels[rand(0, strlen($vowels)-1)];
        else
          $new_str .= strtolower($vowels[rand(0, strlen($vowels)-1)]);
        break;

      default:
        $new_str .= $str[$i];
        break;
    }
  }

  return trim($new_str);
}