PHP Classes

How to Use a PHP Generate Sequence Number Using a Class to Generate the Next Name in a Sequence Using the Package Namecrement PHP: Generate names that follow an incremental pattern

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-06-15 (11 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
namecrement-php 1.0MIT/X Consortium ...7Text processing, PHP 7
Description 

Author

This package can generate names that follow an incremental pattern.

It provides a class that can take as parameter a base name and an array of existing names that follow a pattern that uses a number to define a sequence of incremental names.

The class returns the next name that follows the pattern, adding 1 to the sequence of numbers used in the names in the parameter array.

Picture of Hichem Taboukouyout
Name: Hichem Taboukouyout <contact>
Classes: 1 package by
Country: Algeria Algeria
Innovation award
Innovation award
Nominee: 1x

Instructions

Documentation

Namecrement (PHP)

<!--suppress HtmlDeprecatedAttribute --> <p align="center">

Tests License Packagist Version

</p>

Smart unique name generator for PHP Automatically generates a unique name by incrementing it if needed ? just like "file" ? "file (1)" ? "file (2)" and so on.

? Features

  • Generate unique names based on existing ones
  • Smart gap detection (fills missing indexes first)
  • Lightweight, dependency-free
  • Perfect for filenames, labels, IDs, and more

? Also Available

? Installation

composer require hichemtab-tech/namecrement-php

? Usage

<?php

use HichemTabTech\Namecrement\Namecrement;

$existing = ['file', 'file (1)', 'file (2)'];
$newName = Namecrement::namecrement('file', $existing);

echo $newName; 
// Outputs: "file (3)"

? Advanced Usage

You can customize the suffix format using the %N% placeholder to define how the number is added:

Namecrement::namecrement('file', ['file', 'file -1-', 'file -2-'], ' -%N%-');
// ? 'file -3-'

Namecrement::namecrement('version', ['version', 'version<v1>'], '<v%N%>');
// ? 'version<v2>'

? Suffix Format Rules

  • The `suffixFormat` must include `%N%`, or an `InvalidArgumentException` will be thrown.
  • `%N%` will be replaced by the next available number.
  • Default format is `" (%N%)"`.

| Format Example | Result | |-------------------|---------------------| | " (%N%)" | file (1) | | -%N% | file-1 | | _<v%N%> | file_<v1> | | _%N%_ | file_1_ |

? API

namecrement(string $baseName, array $existingNames): string

| Parameter | Type | Description | |------------------|---------------|------------------------------------------------------| | baseName | string | Proposed name to start from | | existingNames | string[] | List of already existing names | | suffixFormat | string | Optional format for suffix (optional) | | startingNumber | int \| null | The starting number for incrementing (default: null) |

Returns the next available unique name.

? Examples

Namecrement::namecrement('report', ['report', 'report (1)']);
// ? 'report (2)'

Namecrement::namecrement('image', ['photo', 'image', 'image (1)', 'image (2)']);
// ? 'image (3)'

Namecrement::namecrement('new', []);
// ? 'new'
Namecrement::namecrement('file', ['file', 'file (1)', 'file (2)'], ' -%N%-');
// ? 'file -1-'

Namecrement::namecrement('file', [], startingNumber: 5);
// ? 'file (5)'

? License

This project is open-source and available under the MIT license.

? Contributing

Contributions are welcome! Please check out the CONTRIBUTING.md for guidelines.


  Files folder image Files (31)  
File Role Description
Files folder image.github (3 files, 2 directories)
Files folder image.idea (8 files, 2 directories)
Files folder imagesrc (1 file)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files (31)  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (4 files)
Files folder imageworkflows (2 files)
  Accessible without login Plain text file dependabot.yml Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data

  Files folder image Files (31)  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file bug_report.md Data Auxiliary data
  Accessible without login Plain text file documentation.md Data Auxiliary data
  Accessible without login Plain text file feature_request.md Data Auxiliary data
  Accessible without login Plain text file question.md Data Auxiliary data

  Files folder image Files (31)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file auto-assign.yml Data Auxiliary data
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files (31)  /  .idea  
File Role Description
Files folder imagedictionaries (1 file)
Files folder imageinspectionProfiles (1 file)
  Accessible without login Plain text file deployment.xml Data Auxiliary data
  Accessible without login Plain text file laravel-idea.xml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file Namecrement-php.iml Data Auxiliary data
  Accessible without login Plain text file php-test-framework.xml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file phpspec.xml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data

  Files folder image Files (31)  /  .idea  /  dictionaries  
File Role Description
  Accessible without login Plain text file project.xml Data Auxiliary data

  Files folder image Files (31)  /  .idea  /  inspectionProfiles  
File Role Description
  Accessible without login Plain text file profiles_settings.xml Data Auxiliary data

  Files folder image Files (31)  /  src  
File Role Description
  Plain text file Namecrement.php Class Class source

  Files folder image Files (31)  /  tests  
File Role Description
Files folder imageFeature (1 file)
Files folder imageUnit (1 file)
  Plain text file Pest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files (31)  /  tests  /  Feature  
File Role Description
  Accessible without login Plain text file ExampleTest.php Test Example script

  Files folder image Files (31)  /  tests  /  Unit  
File Role Description
  Accessible without login Plain text file NamecrementTest.php Test Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0