# Template Format Used by SunLicense for Key Generation: Complete Guide

> Learn the SunLicense template format for key generation. Understand how letters, digits, and separators create unique license keys for your software.

- Repository: [Mehmet Selcuk Batal/php-license-key-generator](https://github.com/msbatal/php-license-key-generator)
- Tags: deep-dive
- Published: 2026-03-07

---

**SunLicense uses a pattern-based template format where alphabetic characters (A-Z, a-z) represent random letters, digits (0-9) represent random numbers, and any other characters (like hyphens) are inserted verbatim as separators.**

The `msbatal/php-license-key-generator` repository implements this template system in the `SunLicense` class to generate structured license keys. The **template format used by SunLicense for key generation** acts as a blueprint that defines both the length and the visual structure of every generated key, including where random alphanumeric characters appear and where static separators divide the segments.

## Understanding the SunLicense Template Syntax

SunLicense interprets templates using three distinct symbol categories. Each character in the template string maps to a specific generation rule:

- **Alphabetic characters (`A-Z` or `a-z`)** — Generate a random letter. The case of the generated letter follows the `$case` property, which defaults to `upper` but can be set to `lower`.
- **Digit characters (`0-9`)** — Generate a random numeric digit from 0 to 9.
- **Any other character** — Inserted exactly as written. Hyphens (`-`) are commonly used here to create visual separators between key segments.

### Default Template Structure

In [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) at line 31, the class declares a default template that produces a standard 25-character key divided into four groups:

```php
private $template = 'X9XX99-XX99-9X9X-99XX9X';

```

This template generates keys matching the pattern `XXXXXX-XXXX-XXXX-XXXXXX`, where each `X` represents a random letter and each `9` represents a random digit. For example, a generated key might look like `AB7CD9-EF12-3GH4-56IJ7K`.

## How SunLicense Generates Keys from Templates

The generation logic resides in the `license()` method (lines 84–96 of [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php)). This method iterates over every character in the template string and applies conditional logic based on regular expression matching:

1. **Letter detection** — If the character matches `/[a-zA-Z]/`, the method generates a random letter using `chr(rand(65,90))` for uppercase or `chr(rand(97,122))` for lowercase.
2. **Digit detection** — If the character matches `/\d/`, the method generates a random digit using `rand(0,9)`.
3. **Separator preservation** — Any character that fails both checks (such as hyphens) is appended to the key unchanged.

This approach ensures that the **template format used by SunLicense for key generation** is both flexible and deterministic—you control the exact length and visual grouping of keys while the randomness fills in the alphanumeric placeholders.

## Practical Code Examples

### Generate a Single Key with the Default Template

The following example uses the default uppercase template defined in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php):

```php
require 'SunLicense.php';

$generator = new SunLicense();
$key = $generator->generate();
echo $key;  // Output: AB7CD9-EF12-3GH4-56IJ7K

```

### Create Lowercase Keys with a Custom Template

To generate lowercase keys with a specific pattern, pass a custom template and set the case parameter to `lower`:

```php
require 'SunLicense.php';

$customTemplate = 'xx-99-XX-999';
$generator = new SunLicense(null, $customTemplate, 'lower', 5);
$keys = $generator->generate();
print_r($keys);
// Output: Array containing 5 keys like "ab-12-cd-345"

```

### Add a Product Prefix to Generated Keys

You can prepend a static product code to every generated key by passing a prefix as the first constructor argument:

```php
require 'SunLicense.php';

$generator = new SunLicense('PROD');
$key = $generator->generate();
echo $key;  // Output: PROD-AB7CD9-EF12-3GH4-56IJ7K

```

## Summary

- SunLicense uses a **template string** to define the structure of generated license keys, where alphabetic characters represent random letters, digits represent random numbers, and all other characters appear as literal separators.
- The default template `'X9XX99-XX99-9X9X-99XX9X'` produces a 25-character key divided into four hyphen-separated groups.
- The `license()` method in [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php) (lines 84–96) implements the generation logic using regular expressions to distinguish between letters (`/[a-zA-Z]/`), digits (`/\d/`), and separator characters.
- Developers can customize the template format, change the letter case (upper or lower), add prefixes, and generate multiple keys in a single call.

## Frequently Asked Questions

### What characters can I use in a SunLicense template?

You can use any combination of **alphabetic characters** (A-Z, a-z), **digits** (0-9), and **special characters** in your template. Letters generate random alphabetic characters, digits generate random numbers, and any other character (such as hyphens, dots, or underscores) is inserted exactly as written to serve as a separator or delimiter.

### How do I create lowercase license keys with SunLicense?

To generate lowercase keys, instantiate the `SunLicense` class with the third parameter set to `'lower'`. The constructor signature is `SunLicense($prefix, $template, $case, $count)`, so you would call `new SunLicense(null, 'XXXX-XXXX', 'lower', 1)`. When the case is set to lower, alphabetic template characters produce random lowercase letters using `chr(rand(97,122))` instead of the default uppercase `chr(rand(65,90))`.

### Can I use custom separators in the template format?

Yes, the **template format used by SunLicense for key generation** supports any non-alphanumeric character as a custom separator. While hyphens (`-`) are the most common choice for readability, you can use dots (`.`), underscores (`_`), slashes (`/`), or any other symbol. These characters are preserved verbatim in the final output, allowing you to create keys formatted like `XXXX.XXXX.XXXX` or `XX_XX_XX_XX` depending on your requirements.

### Where is the license generation logic implemented in the source code?

The core generation logic is implemented in the `license()` method located at **lines 84–96** of [`SunLicense.php`](https://github.com/msbatal/php-license-key-generator/blob/main/SunLicense.php). This method iterates over each character of the template string, applies regular expression checks to determine if the character is a letter (`/[a-zA-Z]/`) or digit (`/\d/`), and generates the corresponding random character using `chr()` and `rand()` functions. The default template itself is defined as a private property at **line 31** of the same file.