How to useManual Conversion in PHP
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 = 'WelcomeTow3wiki' ; $snakeCase = camelToSnake( $camelCase ); echo $snakeCase ; ?> |
Output
welcome_to_geeks_for_geeks
How to Convert Camel Case to Snake Case in PHP ?
Given a Camel Case String, the task is to convert the Camel Case String to a Snake Case String in PHP.
Examples:
Input: w3wiki
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.
Table of Content
- Using Manual Conversion
- Using Regular Expressions