Open In App

How to Convert Camel Case to Snake Case in PHP ?

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Camel Case String, the task is to convert the Camel Case String to a Snake Case String in PHP.

Examples:

Input: GeeksForGeeks
Output: geeks_for_geeks
Input: WelcomeToGfg 
Output: welcome_to_gfg

Camel case uses capital letters at the beginning of each word except the first one, while snake case separates words with underscores and uses all lowercase letters.

Approach 1: Using Manual Conversion

The simplest way to convert camel case to snake case is to do it manually by iterating through each character of the string and inserting an underscore before each capital letter.

PHP
<?php 

function camelToSnake($camelCase) { 
	$result = ''; 

	for ($i = 0; $i < strlen($camelCase); $i++) { 
		$char = $camelCase[$i]; 

		if (ctype_upper($char)) { 
			$result .= '_' . strtolower($char); 
		} else { 
			$result .= $char; 
		} 
	} 

	return ltrim($result, '_'); 
} 

// Driver code 
$camelCase = 'WelcomeToGeeksForGeeks'; 
$snakeCase = camelToSnake($camelCase); 
echo $snakeCase; 

?>

Output
welcome_to_geeks_for_geeks

Approach 2: Using Regular Expressions

Regular expressions provide a concise way to achieve the conversion from camel case to snake case.

PHP
<?php 

function camelToSnake($camelCase) { 
	$pattern = '/(?<=\\w)(?=[A-Z])|(?<=[a-z])(?=[0-9])/'; 
	$snakeCase = preg_replace($pattern, '_', $camelCase); 
	return strtolower($snakeCase); 
} 

// Driver code 
$camelCase = 'WelcomeToGeeksForGeeks'; 
$snakeCase = camelToSnake($camelCase); 

echo $snakeCase; 

?>

Output
welcome_to_geeks_for_geeks

Approach 3: Using preg_replace_callback() Function

Another efficient method to convert a Camel Case string to a Snake Case string in PHP is by using the preg_replace_callback() function. This function allows for the replacement of patterns in a string using a callback function, which can be used to handle the conversion process.

Example: Use preg_replace_callback to replace each capital letter with _ and the lowercase version of the letter

PHP
<?php
function camelToSnake($input) {
 
    $snakeCase = preg_replace_callback(
        '/[A-Z]/',
        function($matches) {
            return '_' . strtolower($matches[0]);
        },
        $input
    );
    
    // Remove the leading underscore if it exists and return the result
    return ltrim($snakeCase, '_');
}

// Sample usage
$input1 = "GeeksForGeeks";
$input2 = "WelcomeToGfg";

echo camelToSnake($input1);
echo "\n";
echo camelToSnake($input2);

?>

Output
geeks_for_geeks
welcome_to_gfg

Approach 4: Using strtolower and str_replace Functions

In this approach we are using Using strtolower and str_replace Functions to convert a Camel Case string to a Snake Case string in PHP is to combine the strtolower and str_replace functions. This method involves identifying the position of each uppercase letter, inserting an underscore before it, and then converting the entire string to lowercase.

Example:

PHP
<?php
function camelToSnake($input) {
    // Split the string into an array based on uppercase letters
    $words = preg_split('/(?=[A-Z])/', $input, -1, PREG_SPLIT_NO_EMPTY);
    
    // Join the array elements with an underscore
    $snakeCase = implode('_', $words);
    
    // Convert the resulting string to lowercase
    $snakeCase = strtolower($snakeCase);
    
    return $snakeCase;
}


$input1 = "GeeksForGeeks";
$input2 = "WelcomeToGfg";

echo "Input: $input1\n";
echo "Output: " . camelToSnake($input1) . "\n";

echo "Input: $input2\n";
echo "Output: " . camelToSnake($input2) . "\n";
?>

Output
Input: GeeksForGeeks
Output: geeks_for_geeks
Input: WelcomeToGfg
Output: welcome_to_gfg

Next Article

Similar Reads