How to convert Camel Case to Snake Case in PHP
When working with strings in PHP, it is common to encounter different naming conventions, such as camel case and snake case. Camel case uses capital letters to denote the beginning of each word except the first, while snake case separates words with underscores. Converting between these conventions is necessary for consistency and interoperability. In this blog post, we will explore how to convert camel case strings to snake case in PHP, providing you with a practical solution to handle such transformations.
Different ways to convert Camel Case to Snake Case in PHP
- Using Regular Expressions
- Using Exploding and Implode functions
- Using ucwords and str_replace functions
1. Using Regular Expressions
One approach to convert camel case to snake case is by using Regular Expression. The preg_replace() function in PHP can be utilized to match capital letters and insert underscores before them. Here's an example code snippet:
<?php
function camelToSnakeCase($input) {
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}
$camelCaseString = "camelCaseToSnakeCase";
$snakeCaseString = camelToSnakeCase($camelCaseString);
echo $snakeCaseString;
// Output: camel_case_to_snake_case
?>
2. Using Exploding and Implode functions
Another method is to split the camel case string into an array based on capital letters and then use the implode() function to join the array elements with underscores. Here's an example:
<?php
function camelToSnakeCase($input) {
$words = preg_split('/(?=[A-Z])/', $input);
return strtolower(implode('_', $words));
}
$camelCaseString = "camelCaseToSnakeCase";
$snakeCaseString = camelToSnakeCase($camelCaseString);
echo $snakeCaseString;
// Output: camel_case_to_snake_case
?>
You may also like:
Explode() and Implode() Function in PHP
3. Using ucwords and str_replace functions
An alternative approach involves using the ucwords() function to capitalize the initial letters of each word in the camel case string, followed by the str_replace() function to replace spaces with underscores. Here's an example:
<?php
function camelToSnakeCase($input) {
$spacedString = preg_replace('/(?<!^)[A-Z]/', ' $0', $input);
$capitalizedString = ucwords($spacedString);
return strtolower(str_replace(' ', '_', $capitalizedString));
}
$camelCaseString = "camelCaseToSnakeCase";
$snakeCaseString = camelToSnakeCase($camelCaseString);
echo $snakeCaseString;
// Output: camel_case_to_snake_case
?>
Conclusion
Converting camel case strings to snake case is essential for maintaining consistent naming conventions in your PHP applications. By using techniques like regular expressions, string splitting, and function combinations like preg_replace(), implode(), ucwords(), and str_replace(), you can easily transform camel case strings into snake case format. Having a reliable method for case conversion ensures readability and compatibility when working with various naming conventions in your codebase.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂