erdemuk/case-converter

The StringConverter package provides a convenient way to convert strings or array keys to the desired case format.

v1.3 2023-06-22 17:25 UTC

This package is auto-updated.

Last update: 2025-05-22 21:44:29 UTC


README

CaseConverter is a powerful PHP library that provides a convenient way to convert text between different case formats. It supports popular case formats such as snake_case, kebab-case, camelCase, PascalCase, and space case.

Installation

You can install CaseConverter to your project using Composer:

composer require erdemuk/case-converter

Usage

use CaseConverter\CaseConverter;

$input = 'hello_world';
$converted = CaseConverter::convertCase($input, new KebabCase);

echo $converted; // hello-world

In the above example, the convertCase method is used to convert the text hello_world to kebab-case format.

Supported Letter Case Formats

  • SnakeCase: Example: hello_world
  • KebabCase: Example: hello-world
  • CamelCase: Example: helloWorld
  • PascalCase: Example: HelloWorld
  • SpaceCase: Example: Hello World

Text Conversion

CaseConverter can convert a single text or texts within an array. Below are some usage examples:

Converting a Single Text

use CaseConverter\CaseConverter;

$input = 'hello_world';
$converted = CaseConverter::convertCase($input, new KebabCase);

echo $converted; // hello-world

Converting Texts within an Array

use CaseConverter\CaseConverter;

$input = [
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email_address' => 'john.doe@example.com'
];

$converted = CaseConverter::convertCase($input, new PascalCase);

print_r($converted);
/*
Array
(
    [FirstName] => John
    [LastName] => Doe
    [EmailAddress] => john.doe@example.com
)
*/

In the above example, the texts within the array are converted to the 'PascalCase' format.

Converting Multidimensional Arrays

CaseConverter can also handle multidimensional arrays. Here's an example:

use CaseConverter\CaseConverter;

$input = [
    'user' => [
        'firstName' => 'John',
        'lastName' => 'Doe',
        'emailAddress' => 'john.doe@example.com'
    ],
    'address' => [
        'street' => '123 Main St',
        'city' => 'New York',
        'country' => 'USA'
    ]
];

$converted = CaseConverter::convertCase($input, new SnakeCase);

print_r($converted);
/*
Array
(
    [user] => Array
        (
            [first_name] => John
            [last_name] => Doe
            [email_address] => john.doe@example.com
        )

    [address] => Array
        (
            [street] => 123 Main St
            [city] => New York
            [country] => USA
        )

)
*/

In the above example, the texts within a multidimensional array are converted to the 'snake_case' format.

Contribution

Contributions to CaseConverter are always welcome! If you find any bugs, issues, or have suggestions for improvement, please feel free to open an issue or submit a pull request on the GitHub repository.