MultiDev
MultiDev

Reputation: 10649

PHP: Using regex to create string

Is there any way to use a regex expression to create a string, instead of just finding if a match exists?

I have a function to create a random string of characters:

public function getRandomString($length=8) {

  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $string = '';

  for ($p = 0; $p < $length; $p++) {
    $string .= $characters[mt_rand(0, strlen($characters)-1)];
  }

  return $string;

} // End

I was wondering if there was any way to use a regex expression like /[a-zA-Z0-9] instead of typing out all the characters...?

Upvotes: 1

Views: 42

Answers (1)

Jan
Jan

Reputation: 43169

Have a look at range():

$letters = range('0', 'Z');

Upvotes: 1

Related Questions