erdemuk / case-converter
The StringConverter package provides a convenient way to convert strings or array keys to the desired case format.
Installs: 1 004
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^10.2
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.