PHP code example of swindon / code-generator

1. Go to this page and download the library: Download swindon/code-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swindon / code-generator example snippets


/**
 * Generates a random code
 *
 * @param   $length             int
 * @return string
 */
->generate(int $length = 8) : string

(new CodeGenerator)->generate(12);
// output: 4CF9O7XP

(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALPHA)->generate(8);
// output: JTFRujQJ

(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALL)->generate(10);
// output: J\TTu@<ab4

(new CodeGenerator)->setCharacters('ABCXYZabcxyz')->generate(6);
// output: XcZxbc

/**
 * Generates an array of random codes
 *
 * @param   $maxNum             int
 * @param   $length             int
 * @return  array
 */
->bulk(int $maxNum, int $length = 8) : array

(new CodeGenerator)->bulk(6)
// output: [
//   "d3MEJqNq",
//   "Dr4mYKxP",
//   "J0RqiCJ9",
//   "NZuaPUVC",
//   "aZgMjn2m",
//   "EnyQKrat",
// ];

(new CodeGenerator)->bulk(3, 6)
// output: [
//   "OsphQo",
//   "LPKCs2",
//   "DCv0QS",
// ];

(new CodeGenerator)->setCharacters('ABCXYZabcxyz')->bulk(6)
// output: [
//   "xcXAcYbb",
//   "YZXAYBBX",
//   "ZZzYxZyy",
//   "ZYZacACa",
//   "BYzzBYaB",
//   "xZxbacAz",
// ];

(new CodeGenerator)->setCharacters('ABC')->bulk(100, 3);
// throws: 'Cannot generate more than 27 possible unique codes. Try increasing the code length.'