Skip to content

Latest commit

 

History

History
67 lines (40 loc) · 1.38 KB

File metadata and controls

67 lines (40 loc) · 1.38 KB

RandomHelper

The RandomHelper provides a simple static method to generate a random string of a given length using a configurable set of characters (the "haystack").


Basic usage

use Seworqs\Commons\String\Helper\RandomHelper;

$random = RandomHelper::createRandomString();

echo $random; // e.g. "a8pGk2Lm09" (10 characters by default)

Custom length

$random = RandomHelper::createRandomString(20);

echo strlen($random); // 20

Custom haystack (character set)

$random = RandomHelper::createRandomString(12, 'ABC123');

echo $random; // Only uses characters A, B, C, 1, 2, 3

Avoiding confusing characters

To avoid ambiguous characters (like 0 vs O, or I vs 1), provide a clean haystack:

$random = RandomHelper::createRandomString(8, 'abcdefghjkmnpqrstuvwxyz23456789');

echo $random; // e.g. "g7mqfhkp"

This is useful for display codes or visual tokens where clarity matters.


Exception: empty haystack

Passing an empty string as haystack will throw an InvalidArgumentException:

RandomHelper::createRandomString(5, ''); // throws

Notes

  • Characters are selected with random_int() for cryptographic strength.
  • Duplicates may occur. If you need uniqueness, add your own validation or collision check.
  • This helper is ideal for generating simple tokens, codes, or placeholders.