12

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • You can preg replace capitals with space followed by match and then capitalize the first letter – pvg Mar 08 '17 at 07:11
  • http://stackoverflow.com/questions/4519739/split-camelcase-word-into-words-with-php-preg-match-regular-expression – Nodir Rashidov Mar 08 '17 at 07:13
  • preg_replace('/(?<!\ )[A-Z]/', ' $0', $String); will check for the capital letter in the string and insert space. Then by using ucwords you can make it camel case. please check my answer – Sagar Arora Mar 08 '17 at 07:24

9 Answers9

18

a) You can use ucwords():-

echo ucwords($string);

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

b) In your expected outcome spaces are there, if you want that then do:

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

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
4

try this

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

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

echo ucwords($string);

output will be

Create Website Management Users

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,743
  • 1
  • 10
  • 19
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,072
  • 12
  • 24
3
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,658
  • 6
  • 37
  • 54
1

Try this:

preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);
Hiren Makwana
  • 1,976
  • 2
  • 13
  • 28
0

100% Most Efficient :

$word = 'camelCase'; // expected: Camel Case
$sentence = modifyWord($word);

function modifyWord($word)
{
    $splittedWord = str_split($word);
    $modifiedSentence = ucwords($splittedWord[0]);

    for($i = 1; $i < count($splittedWord); $i++){

        // ASCII : A => 65, Z => 90
        // check if the letter is between A & Z

        if(ord($splittedWord[$i]) >= 65 && ord($splittedWord[$i]) <= 90){
            $modifiedSentence .= ' '.$splittedWord[$i];
        }else{
            $modifiedSentence .= $splittedWord[$i];
        }
    }
    
    return $modifiedSentence;
}
SAKIB
  • 475
  • 5
  • 7
0

Fixed version with UPPER letter check and symbols

<?php
$word = 'UniCredit Bank (AA)'; // expected: Camel Case
$word = 'CamelCase with space (CAPITAL_NAME) and strangEEword'; // expected: Camel Case
$sentence = modifyWord($word);

echo $sentence;

function modifyWord($word)
{
    $splittedWord = str_split($word);
    $modifiedSentence = ucwords($splittedWord[0]);

    for($i = 1; $i < count($splittedWord); $i++){

        // ASCII : A => 65, Z => 90
        // check if the letter is between A & Z
        
        $nextCapital = isset($splittedWord[$i+1]) && ord($splittedWord[$i+1]) >= 65 && ord($splittedWord[$i+1]) <= 90;
        $prevCapital = isset($splittedWord[$i-1]) && ord($splittedWord[$i-1]) >= 65 && ord($splittedWord[$i-1]) <= 90;
        

        if(ord($splittedWord[$i]) >= 65 && ord($splittedWord[$i]) <= 90 && !$nextCapital && !$prevCapital){
            $modifiedSentence .= ' '.$splittedWord[$i];
        }else{
            $modifiedSentence .= $splittedWord[$i];
        }
    }
    
    return $modifiedSentence;
}
r1si
  • 1,136
  • 2
  • 19
  • 34