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?
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
Use below code to solve:
$String = 'createWebsiteManagementUsers';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
echo ucwords($Words);
//output will be Create Website Mangement Users
try this
$data = preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers');
$string = implode(' ', $data);
echo ucwords($string);
output will be
Create Website Management 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
尝试这个:
preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.