php function to convert string to camelcase 1

php function to convert string to camelcase

public static function camelCase($str, array $noStrip = [])
{
        // non-alpha and non-numeric characters become spaces
        $str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str);
        $str = trim($str);
        // uppercase the first character of each word
        $str = ucwords($str);
        $str = str_replace(" ", "", $str);
        $str = lcfirst($str);

        return $str;
}

Here is what the above code is Doing:
1. It’s taking the string and replacing all non-alpha and non-numeric characters with spaces.
2. It’s trimming the string.
3. It’s uppercasing the first character of each word.
4. It’s removing all spaces.
5. It’s lowercasing the first character of the string.

Similar Posts