Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert camel case to upper english words in php

Tags:

php

I have different strings that are function names like

createWebsiteManagementUsers

I want to change them into

Create Website Mangement Users

How can i achieve that in PHP?

like image 987
Ali Zia Avatar asked Mar 08 '17 07:03

Ali Zia


People also ask

How do you capitalize words in PHP?

The strtoupper() function converts a string to uppercase. Note: This function is binary-safe. Related functions: strtolower() - converts a string to lowercase.

What is CamelCase conversion?

Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. It indicates the separation of words with a single capitalized letter, and the first word starting with either case.

How can I tell if a string is CamelCase?

You should use the @supercharge/strings package to determine whether a given string is in camelCase format.


1 Answers

You can use ucwords():-

echo ucwords($string);

Output:- https://3v4l.org/sCiEJ

Note:- In your expected outcome spaces comes? Do you want that too?

If Yes then use:-

echo ucwords(implode(' ',preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers')));

Example with Output:- https://3v4l.org/v3KUK

like image 92
Anant Kumar Singh Avatar answered Oct 16 '22 14:10

Anant Kumar Singh