Sai A Sai A
Updated date Jul 25, 2023
In this blog, we will learn the art of converting strings to Camel Case in PHP. In this blog, we explore multiple methods to achieve this task, from basic explode() and ucfirst() functions to the power of regular expressions and str_replace() combined with ucwords().
  • 2.2k
  • 0
  • 0

Introduction:

Camel Case is a popular naming convention where words are concatenated without spaces or underscores, and each word's first letter (except the first one) is capitalized. This process is vital for maintaining coding standards, creating clean URLs, or formatting data for user-friendly displays. In this blog, we will explore multiple methods to achieve this task in PHP.

Method 1: Using Explode and Ucfirst Functions

Our first method utilizes the built-in PHP functions explode() and ucfirst(). The idea is to split the input string into words using a delimiter (such as space or underscore), capitalize the first letter of each word, and then concatenate them to form the Camel Case representation.

function toCamelCaseMethod1($inputString) {
    $words = explode(' ', $inputString);
    $camelCase = $words[0];

    for ($i = 1; $i < count($words); $i++) {
        $camelCase .= ucfirst($words[$i]);
    }

    return $camelCase;
}

// Example Usage:
$inputString = "convert this_string to camel case";
echo toCamelCaseMethod1($inputString);

Output:

convertThis_stringToCamelCase

Method 2: Using Regular Expressions

In the second method, we leverage the power of regular expressions to accomplish the task more concisely. We use the preg_replace_callback() function to capitalize each word and remove spaces or underscores.

function toCamelCaseMethod2($inputString) {
    return preg_replace_callback(
        '/(?:^|_| )(\w)/',
        function ($matches) {
            return strtoupper($matches[1]);
        },
        $inputString
    );
}

// Example Usage:
$inputString = "convert this_string to camel case";
echo toCamelCaseMethod2($inputString);

Output:

convertThisStringToCamelCase

Method 3: Utilizing str_replace() and ucwords() Functions

For our third approach, we employ the str_replace() and ucwords() functions. First, we remove spaces or underscores and then capitalize the first letter of each word.

function toCamelCaseMethod3($inputString) {
    $intermediate = str_replace(['_', ' '], '', ucwords($inputString, '_ '));
    return lcfirst($intermediate);
}

// Example Usage:
$inputString = "convert this_string to camel case";
echo toCamelCaseMethod3($inputString);

Output:

convertThisStringToCamelCase

Conclusion:

In this blog, we explored three different methods to convert strings to Camel Case in PHP. We started with a simple yet effective approach using explode() and ucfirst(). Then, we delved into a more concise method with regular expressions, offering a balance between code length and performance. Lastly, we demonstrated another solution utilizing str_replace() and ucwords(), which might be preferable for certain use cases.

Comments (0)

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