9

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?

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Ali Zia
  • 3,687
  • 3
  • 24
  • 68

7 Answers7

11

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

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
4

Use below code to solve:

$String = 'createWebsiteManagementUsers';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
echo ucwords($Words);

//output will be Create Website Mangement Users
Sagar Arora
  • 1,675
  • 1
  • 9
  • 19
3

try this

$data = preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers');

$string = implode(' ', $data);

echo ucwords($string);

output will be

Create Website Management Users

3

Here is what you need. This has the spaces as well!

function parseCamelCase($camelCaseString){
    $words_splited = preg_split('/(?=[A-Z])/',$camelCaseString);
    $words_capitalized = array_map("ucfirst", $words_splited);
    return implode(" ", $words_capitalized);
}

Thanks

masterFly
  • 1,052
  • 9
  • 23
2
function camelCaseToString($string)
{
    $pieces = preg_split('/(?=[A-Z])/',$string);
    $word = implode(" ", $pieces);
    return ucwords($word);
}

$name = "createWebsiteManagementUsers";
echo camelCaseToString($name);
1

May be you can try something like this

//Split words with Capital letters
$pieces = preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers');

$string = implode(' ', $pieces);

echo ucwords($string);

//You will get your desire output Create Website Management Users

vijaykumar
  • 4,342
  • 6
  • 38
  • 53
1

Try this:

preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);
Hiren Makwana
  • 1,800
  • 2
  • 12
  • 26