1. Introduction

The RandomStringManager is an easy to use tool of the APF to create quick and easy random formatted and non-formatted hashes and strings. The developer can configure the chars to use for creating the hash and the length of the resulting hash. If no configuration was given, a default configuration is used for creating the hash.

Symbols and special characters will be encoded as UTF-8. A particular configuration is not available at the moment.

2. Creating random strings

The RandomStringManager is providing 2 methods:

  • createHash()
  • advancedCreateHash()

2.1. The Default Configuration

By default the RandomStringManager is using the following configuration:

  • Chars: 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  • Length: 16

That means: A string will be created with the length of 16 chars out of the possible chars 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.

2.2. Method createHash()

Each time you call the method createHash() the function create and return a random string. The following is an example application:

PHP code
// Initialisation of the manager $randomString = $this->getAndInitServiceObject( 'APF\tools\string\RandomStringManager', array('chars' => '', 'length' => '10') ); // create string $hash = $randomString->createHash();

The code-box above can be interpreted as follows:

For creating the hash the chars 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ are used. This corresponds to the defaultconfiguration because no other configuration is specified. The length was set to 10 chars. A result could be for example kJ9i7zjC98.

2.3. Method advancedCreateHash()

A different application is to generate a unique key for the application. Although this is due to the random generation and a correspondingly large selected length already unlikely, but a double occurrence is possible.

If the developer wants ensure that is the random string only once, the extended advancedCreateHash() method can be used. This function is creating a database query after the hash was created. If the result is true, a different hash is created. Only a negative result is returned by the method. Thus the developer can be sure this is a really unique string.

The following box shows the use of that method:

PHP code
// Initialisation of the manager $randomString = $this->getAndInitServiceObject( 'APF\tools\string\RandomStringManager', array('chars' => '0123456789ABCDEF', 'length' => 3) ); // create string and check with database $hash = $randomString->advancedCreateHash('SELECT id,allHex FROM hexcode WHERE allHex =', 'database-1');

The code-box above can be interpreted as follows:

For the advancedCreateHash() method u have to provide two more parameter. The first statement is the SQL-query to check for an existing string. The second statement defines the ConnectionKey of the database. Details can be found under ConnectionManager.

For creating the hash the chars 0123456789ABCDEF are used. The length is set to 3 chars. A result could be for example 0DF.

3. Creating random string with formatting (serial number)

You can use the RandomStringManager to create string with a given formatting. This could be useful for example:

  • Creating serial numbers
  • Creating coupon codes

3.1. The Default Configuration

By default the RandomStringManager is using the following configuration:

  • Characters: 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  • Delimiter: -
  • Formatting (Scheme): XXX9-XX99-X99X-99XX

The characters used in the scheme an thy modification of the scheme will be explained below:

  • X: big letter on this position
  • x: small letter on this position
  • 9: number on this position
  • -: delimiter on this position
Definition of the delimiter and use of the delimiter must be identical! To change "-" to "." you need first to define the delimiter and then specify the location of it in the scheme!

That means: A serial number containing of four blocks seperated by "-" is generated. Used are only big letters and numbers.

3.2. Method createSerial()

Using the method createSerial() creates a random formatted string by using the standard configuration or your different settings.
To avoid the risk of confusion especially in print media please remember not to use characters that may look similar:
1, i, l, L, 0, O, o

Example of use:

PHP code
$randomString = $this->getAndInitServiceObject('APF\tools\string\RandomStringManager', array()); echo $randomString->createSerial();

The code-box above can be interpreted as follows:

For creating the serial number the default character set is used because no different character set was given: 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. The scheme with delimiter looks as follows: XXX9-XX99-X99X-99XX. A result could be: TZE4-YM12-Q88B-97QA

Another example in which according to the following serial number the configuration will be explained:

PHP code
$config = array( 'chars' => '23456789abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', 'delimiter' => '.', 'scheme' => 'x.xx.xxx.9999.XXXX' ); $randomString = $this->getAndInitServiceObject('APF\tools\string\RandomStringManager', $config); echo $randomString->createSerial();

To avoid the risk of confusion by using characters that look similar a character set without this characters is used.
"." (Point) is used as delimiter.
The scheme is set to "x.xx.xxx.9999.XXXX".

4. Obtain RandomStringManager as DIService

The RandomStringManager can also be obtained using the DIServiceManager.


PHP code
$string = $this->getDIServiceObject('VENDOR\services', 'string-service'); // Create hash echo $string->createHash(); // Create serial number echo $string->createSerial();

Example configuration:

APF configuration
[string-service] class = "APF\tools\string\RandomStringManager" servicetype = "SINGLETON" ; necessary configuration for creating random strings and serial numbers conf.chars.method = "setChars" conf.chars.value = "23456789abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ" conf.length.method = "setLength" conf.length.value = "16" ; necessary configuration for creating serial numbers conf.delimiter.method = "setDelimiter" conf.delimiter.value = "-" conf.scheme.method = "setScheme" conf.scheme.value = "XXX9-XX99-X99X-99XX" ; X = big letter, , x = small letter, 9 = number, - = delimiter (must be equal to value of method setDelimiter!!) ; necessary configuration to check existing random strings in database conf.conn.method = "setConnectionKey" conf.conn.value = "database-2"

Possible result:
Serial number: AR2-PH26-L88Q-45MC
For further information about the DIServiceManager please read the manual an the reviews in the Wiki.

A discussion about the birth of the RandomStringManager with problems of using special characters: (German only)


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.

In order to provide a state-of-the-art web experience and to continuously improve our services we are using cookies. By using this web page you agree to the use of cookies. For more information, please refer to our Privacy policy.