9951 explained code solutions for 126 technologies


php-fakerHow do I generate alphanumeric strings using PHP Faker?


You can generate alphanumeric strings using the Faker\Generator::lexify() method provided by the PHP Faker library. This method takes a specified character mask as a parameter and returns a random string of characters that fits the mask.

For example, the following code will generate a random string of 8 alphanumeric characters:

<?php

require_once 'vendor/autoload.php';

$faker = Faker\Factory::create();

echo $faker->lexify('????????'); // Outputs a string like "u2h3c7v2"

The character mask is composed of the following characters:

  • ? - Represents a random alphanumeric character.
  • # - Represents a random digit.
  • * - Represents a random alphabetic character.

You can combine these characters to create any kind of alphanumeric strings. For example, the following code will generate a random string of 8 characters containing at least one digit:

<?php

require_once 'vendor/autoload.php';

$faker = Faker\Factory::create();

echo $faker->lexify('??????##'); // Outputs a string like "a3h9d6s2"

You can find more information about the Faker\Generator::lexify() method in the PHP Faker documentation.

Edit this code on GitHub