https://github.com/seothemes/php-case-converter
PHP function to convert a string to another naming convention
https://github.com/seothemes/php-case-converter
camelcase case-converter php snakecase
Last synced: 12 months ago
JSON representation
PHP function to convert a string to another naming convention
- Host: GitHub
- URL: https://github.com/seothemes/php-case-converter
- Owner: seothemes
- License: gpl-2.0
- Created: 2020-04-03T08:35:06.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-03T08:50:14.000Z (about 6 years ago)
- Last Synced: 2025-05-29T20:47:08.346Z (about 1 year ago)
- Topics: camelcase, case-converter, php, snakecase
- Language: PHP
- Size: 17.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Case Converter
Simple PHP function that takes a string and converts it to another naming convention.
### The code
```php
/**
* Converts a string to different naming conventions.
*
* @since 1.0.0
*
* @author Lee Anthony https://seothemes.com
*
* @param string $string String to convert.
* @param string $case Naming convention.
*
* @return string
*/
function convert_case( $string, $case = 'snake' ) {
$delimiters = 'sentence' === $case ? [ ' ', '-', '_' ] : [ ' ', '-', '_', '.' ];
$lower = trim( str_replace( $delimiters, $delimiters[0], strtolower( $string ) ), $delimiters[0] );
$upper = trim( ucwords( $lower ), $delimiters[0] );
$pieces = explode( $delimiters[0], $lower );
$cases = [
'camel' => lcfirst( str_replace( ' ', '', $upper ) ),
'pascal' => str_replace( ' ', '', $upper ),
'snake' => strtolower( implode( '_', $pieces ) ),
'ada' => str_replace( ' ', '_', $upper ),
'macro' => strtoupper( implode( '_', $pieces ) ),
'kebab' => strtolower( implode( '-', $pieces ) ),
'train' => lcfirst( str_replace( ' ', '-', $upper ) ),
'cobol' => strtoupper( implode( '-', $pieces ) ),
'lower' => strtolower( $string ),
'upper' => strtoupper( $string ),
'title' => $upper,
'sentence' => ucfirst( $lower ),
'dot' => strtolower( implode( '.', $pieces ) ),
];
return $cases[ $case ];
}
```
### How to use
Copy and paste the helper function into your project, then use it:
```php
echo convert_case( 'This is_an example-string.', 'camel' ); // Outputs: thisIsAnExampleString
```
### Available cases
| Case | Example |
| -------- | ------- |
| camel | myNameIsBond |
| pascal | MyNameIsBond |
| snake | my_name_is_bond |
| ada | My_Name_Is_Bond |
| macro | MY_NAME_IS_BOND |
| kebab | my-name-is-bond |
| train | My-Name-Is-Bond |
| cobol | MY-NAME-IS-BOND |
| lower | my name is bond |
| upper | MY NAME IS BOND |
| title | My Name Is Bond |
| sentence | My name is bond |
| dot | my.name.is.bond |