How to Convert String to Camelcase in PHP? Last Updated : 11 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a String containing spaces, the task is to Convert String to Camelcase in PHP. Converting a string to CamelCase is a common operation in PHP, especially when working with variable names or class names. CamelCase is a naming convention where the first letter of each word in a compound word is capitalized except for the initial word. In this article, we will cover all possible approaches to convert a string to CamelCase in PHP.Table of ContentApproach 1: Using ucwords() and str_replace() FunctionsApproach 2: Using preg_replace_callback() FunctionApproach 3: Using ucwords() and strtr() FunctionsApproach 4: Using explode and implodeApproach 1: Using ucwords() and str_replace() FunctionsOne method to convert a string to CamelCase is by using the ucwords() function to capitalize the first letter of each word, and then using str_replace() to remove the spaces. PHP <?php function camelCase($string) { $string = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $string)) ); return $string; } // Driver Code $str = 'welcome to geeks-for-geeks'; $camelCase = camelCase($str); echo $camelCase; ?> OutputWelcomeToGeeksForGeeks Explanation:First replace underscores and hyphens with spaces using str_replace() function.Then capitalize the first letter of each word using ucwords() function.Approach 2: Using preg_replace_callback() FunctionAnother approach is to use preg_replace_callback() function with a regular expression to match each word and capitalize the first letter. PHP <?php function camelCase($string) { return preg_replace_callback('/(?:^|_| )(.?)/', function($matches) { return strtoupper($matches[1]); }, $string); } // Driver Code $str = 'welcome to geeks for geeks'; $camelCase = camelCase($str); echo $camelCase; ?> OutputWelcomeToGeeksForGeeksExplanation:The regular expression /(?:^|_)(.?)/ matches the beginning of the string or an underscore followed by any character.The preg_replace_callback() function is used to apply a callback function to each match, converting the matched character to uppercase.Approach 3: Using ucwords() and strtr() FunctionsThis approach uses strtr() functions to replace underscores with spaces, then use ucwords() to capitalize each word, and finally str_replace() to remove spaces. PHP <?php function camelCase($string) { $string = ucwords( strtr($string, ['_' => ' ', '-' => ' ']) ); $string = str_replace(' ', '', $string); return $string; } // Driver Code $str = 'welcome to geeks for geeks'; $camelCase = camelCase($str); echo $camelCase; ?> OutputwelcomeToGeeksForGeeksExplanation:strtr is used to replace underscores and hyphens with spaces.Finally, we remove spaces and lowercase the first letter of the resulting string.Approach 4: Using explode and implodeIn this approach we split the string into an array of words using explode and capitalize the first letter of each word except the first one and then join them back into a single camelCase string using implode. PHP <?php $string = "welcome to geekforgeeks"; $words = explode(' ', $string); $camelCaseString = $words[0] . implode('', array_map('ucfirst', array_slice($words, 1))); echo $camelCaseString; ?> OutputwelcomeToGeekforgeeks Comment More infoAdvertise with us Next Article How to Convert String to Camelcase in PHP? B blalverma92 Follow Improve Article Tags : PHP Similar Reads How to Convert a String to Lower or Upper Case in Ruby? In this article, we will discuss how to convert a string to lower or upper case in Ruby. We can convert a string to lower or upper case using built-in methods provided in the Ruby language. Table of Content Using DowncaseUsing UpcaseUsing CapitalizeUsing DowncaseThe downcase method is used to conver 2 min read JavaScript - Convert String to Title Case Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript.1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and t 4 min read How to Extract Words from given String in PHP ? Extracting words from a given string is a common task in PHP, often used in text processing and manipulation. Below are the approaches to extract words from a given string in PHP: Table of Content Using explode() functionUsing regular expressionsUsing explode() functionIn this approach, we are using 1 min read How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re 3 min read Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right 2 min read Like