Sai A Sai A
Updated date Jul 25, 2023
In this blog, we will learn various methods to convert strings to snake case in PHP. Discover custom functions and built-in PHP methods to format strings efficiently. Improve code readability and maintainability using snake case, a popular naming convention for variables, functions, and database fields.
  • 2.8k
  • 0
  • 0

Introduction:

In PHP development, working with strings is a common task, and one frequently encountered formatting requirement is converting strings from various formats to snake case. Snake case, also known as underscore case, is a popular convention for naming variables, functions, and database fields. It involves replacing spaces and special characters with underscores and converting the entire string to lowercase. In this blog, we will explore multiple methods to achieve this conversion in PHP, comparing their performance and suitability for different scenarios.

Method 1: Using Custom Function

The most straightforward approach to convert a string to snake case is by using a custom function. Let's create a PHP function called toSnakeCase() that takes a string as input and returns the snake-case formatted string as output.

function toSnakeCase($inputString) {
    // Replace spaces and special characters with underscores
    $snakeCaseString = preg_replace('/[^A-Za-z0-9]+/', '_', $inputString);
    // Convert to lowercase
    $snakeCaseString = strtolower($snakeCaseString);
    // Remove leading and trailing underscores
    $snakeCaseString = trim($snakeCaseString, '_');
    return $snakeCaseString;
}

Output:

Let's test the toSnakeCase() function with some sample inputs:

echo toSnakeCase("Hello World!"); // Output: hello_world
echo toSnakeCase("Hello, how are you?"); // Output: hello_how_are_you
echo toSnakeCase("  Snake Case   "); // Output: snake_case

The toSnakeCase() function first replaces any spaces and special characters with underscores using a regular expression (preg_replace). Then, it converts the entire string to lowercase (strtolower) and finally trims any leading or trailing underscores.

Method 2: Using str_replace() and strtolower()

Another approach to achieve the same result is by using the built-in PHP functions str_replace() and strtolower().

function toSnakeCase($inputString) {
    // Replace spaces with underscores
    $snakeCaseString = str_replace(' ', '_', $inputString);
    // Replace special characters with underscores
    $snakeCaseString = preg_replace('/[^A-Za-z0-9]+/', '_', $snakeCaseString);
    // Convert to lowercase
    $snakeCaseString = strtolower($snakeCaseString);
    return $snakeCaseString;
}

Output:

Testing the updated toSnakeCase() function:

echo toSnakeCase("Hello World!"); // Output: hello_world
echo toSnakeCase("Hello, how are you?"); // Output: hello_how_are_you
echo toSnakeCase("  Snake Case   "); // Output: snake_case

In this method, we first use str_replace() to replace spaces with underscores. Next, we use the same preg_replace() step as in the previous method to handle special characters. Finally, the string is converted to lowercase using strtolower().

Method 3: Using ucwords() and str_replace()

In some cases, we may encounter input strings with spaces as word separators, but without any special characters. In such situations, we can use a slightly different approach.

function toSnakeCase($inputString) {
    // Convert the string to title case
    $titleCaseString = ucwords($inputString);
    // Replace spaces with underscores
    $snakeCaseString = str_replace(' ', '_', $titleCaseString);
    // Convert to lowercase
    $snakeCaseString = strtolower($snakeCaseString);
    return $snakeCaseString;
}

Output:

Testing the toSnakeCase() function with new input:

echo toSnakeCase("hello world"); // Output: hello_world
echo toSnakeCase("the quick brown fox"); // Output: the_quick_brown_fox
echo toSnakeCase("php is awesome"); // Output: php_is_awesome

In this method, we first use ucwords() to convert the input string to a title case, capitalizing the first letter of each word. Then, we replace the spaces with underscores and convert the entire string to lowercase.

Conclusion:

In this blog, we explored multiple methods to convert a given string to a snake case in PHP. We started with a simple custom function, followed by variations using different built-in PHP functions.

Comments (0)

There are no comments. Be the first to comment!!!