How SunLicense Handles Specific Character Sets for License Keys in PHP
SunLicense generates license keys using a template-based system where alphabetic template characters produce random letters (A–Z or a–z), numeric characters produce random digits (0–9), and any other character is converted to a hyphen, without support for arbitrary custom character sets.
The msbatal/php-license-key-generator repository provides a lightweight PHP class for generating license keys, but its character set handling is strictly defined by internal logic rather than user-configurable alphabets. Understanding how SunLicense processes specific character sets for license keys helps developers determine whether the library meets their formatting requirements or if modifications are necessary.
How SunLicense Processes Character Sets in License Keys
SunLicense implements character set handling in the license() method within SunLicense.php (lines 85–95). The method iterates over a template string and applies specific character generation rules based on the template character type.
Alphabetic Characters and Case Control
When the template contains a letter (A–Z or a–z), SunLicense generates a random alphabetic character using chr(rand(65, 90)) for uppercase or chr(rand(97, 122)) for lowercase. The $case property (line 37) controls this behavior, accepting 'upper' or 'lower' values. By default, the library uses uppercase letters.
Numeric Digits
Template characters representing digits (0–9) trigger the generation of random numeric values between 0 and 9 using rand(0, 9). This occurs in lines 91–92 of SunLicense.php, producing the numeric segments of the license key.
Special Characters and Delimiters
Any character in the template that is neither a letter nor a digit is automatically converted to a hyphen (-). This fallback logic appears in lines 93–95, meaning symbols like *, #, or @ in custom templates will render as hyphens rather than literal characters or random symbols.
Configuration Options for Character Handling
While SunLicense does not support arbitrary character sets, it provides four configurable properties in SunLicense.php that influence key composition:
$prefix(line 25): Prepends a static string to the generated key$template(line 31): Defines the pattern of letters, numbers, and delimiters (default:X9XX99-XX99-9X9X-99XX9X)$case(line 37): Forces alphabetic characters toupperorlowercase$keyCount(line 43): Specifies how many unique keys to generate
Limitations of SunLicense Character Set Support
The current implementation in msbatal/php-license-key-generator imposes specific constraints on character set customization:
- No custom alphabets: You cannot restrict generation to hexadecimal characters (0–9, A–F) or exclude ambiguous characters (I, O, 0, 1).
- No special symbol support: Beyond the hyphen delimiter, you cannot include random symbols like
@,#, or$in the generated key. - Fixed delimiter logic: Any non-alphanumeric template character becomes a hyphen, regardless of the original symbol used.
To implement custom character sets, you must modify the license() method in SunLicense.php or post-process the generated string to replace characters.
Practical Code Examples
The following examples demonstrate how SunLicense handles character sets in real-world usage:
<?php
require_once 'SunLicense.php';
// Default generation with mixed alphanumeric characters
$default = new SunLicense();
echo $default->generate(); // Output: "X5KJ93-AB78-2Q5Z-71XY3"
// Custom template emphasizing specific character positions
$custom = new SunLicense(null, 'AA99-9A9A-A9A9-99AA');
print_r($custom->generate());
// Template: A=letter, 9=digit, hyphen=delimiter
// Output: ["GT12-7L5M-K3H2-58QT"]
// Lowercase character set
$lower = new SunLicense(null, null, 'lower');
echo $lower->generate(); // Output: "p7mj23-xy45-6p9x-23xy6"
// Prefix with standard character handling
$prefixed = new SunLicense('SLK');
echo $prefixed->generate(); // Output: "SLK-X5KJ93-AB78-2Q5Z-71XY3"
// Multiple keys demonstrating consistent character set rules
$multi = new SunLicense(null, null, 'upper', 3);
$keys = $multi->generate();
foreach ($keys as $k) {
echo $k . PHP_EOL;
}
// Template with special symbols (converted to hyphens)
$symbol = new SunLicense(null, 'A*9A*9');
echo $symbol->generate(); // Output: "G-7K-9P" (asterisks become hyphens)
?>
Summary
- SunLicense processes character sets through a template-based system in
SunLicense.php, where letters generate random alphabetic characters, digits generate random numbers, and all other characters become hyphens. - The
$caseproperty controls alphabetic casing (upper or lower), but the library does not support custom character sets like hexadecimal-only or special symbols. - Template customization is limited to positioning letters, numbers, and delimiters; arbitrary symbols in templates are converted to hyphens during generation.
- To implement specific character set restrictions, developers must modify the
license()method or apply post-processing to the generated keys.
Frequently Asked Questions
Can SunLicense generate license keys with hexadecimal characters only?
No, SunLicense does not provide a built-in hexadecimal character set option. The library only supports mixed alphabetic (A–Z or a–z) and numeric (0–9) characters based on template positions. To generate hexadecimal-only keys, you would need to modify the license() method in SunLicense.php to restrict the random range to chr(rand(65, 70)) for letters and rand(0, 9) for digits, or filter the output post-generation.
Why do special symbols in my template become hyphens?
SunLicense explicitly converts any non-alphanumeric template character to a hyphen (-) as a fallback mechanism. According to lines 93–95 in SunLicense.php, the else clause in the template processing loop assigns $key .= "-" for any character that is not a letter or digit. This design choice standardizes delimiters but prevents using symbols like @, #, or * as literal characters or random generators within the key.
How do I change the case of letters in generated license keys?
Set the $case property to either 'upper' or 'lower' when instantiating the SunLicense class, or pass it as the third constructor argument. The license() method checks this property at lines 85–90: when $case is 'upper', it generates chr(rand(65, 90)) (A–Z); when 'lower', it uses chr(rand(97, 122)) (a–z). By default, if no case is specified, the library uses uppercase letters.
Can I use a custom alphabet or exclude ambiguous characters like O and 0?
No, SunLicense does not support custom alphabets or character exclusion lists. The random generation logic in license() uses hardcoded ASCII ranges for all letters (65–90 or 97–122) and digits (0–9). To exclude ambiguous characters like capital O, lowercase l, or the number 0, you would need to override the license() method to implement a filtered random selection or use a post-processing function to replace unwanted characters with alternatives.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →