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?
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?
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
try this
$data = preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers');
$string = implode(' ', $data);
echo ucwords($string);
output will be
Create Website Management Users
Use below code to solve:
$String = 'createWebsiteManagementUsers';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
echo ucwords($Words);
//output will be Create Website Mangement Users
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
function camelCaseToString($string)
{
$pieces = preg_split('/(?=[A-Z])/',$string);
$word = implode(" ", $pieces);
return ucwords($word);
}
$name = "createWebsiteManagementUsers";
echo camelCaseToString($name);
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
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;
}
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;
}