https://github.com/sebastiansulinski/string-converter
Convert string beween different styles / types
https://github.com/sebastiansulinski/string-converter
camelcase hyphens php string-converter underscore
Last synced: 10 months ago
JSON representation
Convert string beween different styles / types
- Host: GitHub
- URL: https://github.com/sebastiansulinski/string-converter
- Owner: sebastiansulinski
- License: mit
- Created: 2015-02-28T20:39:57.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2023-12-10T14:33:46.000Z (about 2 years ago)
- Last Synced: 2025-03-28T04:11:31.739Z (10 months ago)
- Topics: camelcase, hyphens, php, string-converter, underscore
- Language: PHP
- Homepage:
- Size: 67.4 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple string converter
This package allows converting between different string formats.
## Usage examples
### Class-name
```php
use SSD\StringConverter\Factory;
echo Factory::classNameToCamel('PhpEol');
// phpEol
echo Factory::classNameToConstant('PhpEol');
// PHP_EOL
echo Factory::classNameToHyphen('PhpEol');
// Php-Eol
echo Factory::classNameToUnderscore('PhpEol');
// Php_Eol
echo Factory::classNameToSpace('PhpEol');
// Php Eol
```
### Camel-case
```php
use SSD\StringConverter\Factory;
echo Factory::camelToClassName('phpEol');
// PhpEol
echo Factory::camelToConstant('phpEol');
// PHP_EOL
echo Factory::camelToHyphen('phpEol');
// php-Eol
echo Factory::camelToUnderscore('phpEol');
// php_Eol
echo Factory::camelToSpace('phpEol');
// php Eol
```
### Constant
```php
use SSD\StringConverter\Factory;
echo Factory::constantToClassName('PHP_EOL');
// PhpEol
echo Factory::constantToCamel('PHP_EOL');
// phpEol
echo Factory::constantToHyphen('PHP_EOL');
// PHP-EOL
echo Factory::constantToUnderscore('PHP_EOL');
// PHP_EOL
echo Factory::constantToSpace('PHP_EOL');
// PHP EOL
```
### Hyphen
```php
use SSD\StringConverter\Factory;
echo Factory::hyphenToClassName('php-eol');
// PhpEol
echo Factory::hyphenToCamel('php-eol');
// phpEol
echo Factory::hyphenToConstant('php-eol');
// PHP_EOL
echo Factory::hyphenToUnderscore('php-eol');
// php_eol
echo Factory::hyphenToSpace('php-eol');
// php eol
```
### Underscore
```php
use SSD\StringConverter\Factory;
echo Factory::underscoreToClassName('php_eol');
// PhpEol
echo Factory::underscoreToCamel('php_eol');
// phpEol
echo Factory::underscoreToConstant('php_eol');
// PHP_EOL
echo Factory::underscoreToHyphen('php_eol');
// php-eol
echo Factory::underscoreToSpace('php_eol');
// php eol
```
### Space
```php
use SSD\StringConverter\Factory;
echo Factory::spaceToClassName('php eol');
// PhpEol
echo Factory::spaceToCamel('php eol');
// phpEol
echo Factory::spaceToConstant('php eol');
// PHP_EOL
echo Factory::spaceToHyphen('php eol');
// php-eol
echo Factory::spaceToUnderscore('php eol');
// php_eol
```
## Optional second callback argument
Factory methods can take an optional, second argument of `callable` type so you can additionally modify the result:
```php
echo Factory::constantToSpace('PHP_EOL');
// PHP EOL
echo Factory::constantToSpace('PHP_EOL', 'strtolower');
// php eol
echo Factory::constantToSpace('PHP_EOL', function($string) {
return str_replace(' ', '', $string);
});
// phpeol
```