如何搜索某些字符串中的每个大写字符并替换它[复制]

时间:2022-08-19 19:35:55

This question already has an answer here:

这个问题在这里已有答案:

what i'm trying to make a function to change the camelcase variables like "myFullName" to be readable version. for example if i have varible like this

我正在尝试将函数更改为像“myFullName”这样的camelcase变量为可读版本。例如,如果我有这样的变数

$label = "myFullName" 

<!-- expected output is -->

$label = "my full name" ;

1 个解决方案

#1


3  

A different approach to REGEX will be to iterate over the word and check if there is an uppercase letter, insert an space before and lowercase the letter

REGEX的另一种方法是迭代单词并检查是否有大写字母,在字母之前插入空格和小写字母

$label = 'myFullName';

$new_word='';

for($i=0, $n=strlen($label); $i<$n; $i++){

     $new_word .= strtoupper($label[$i]) == $label[$i] 
        ? ' '.strtolower($label[$i]) 
        : $label[$i];
}

echo $new_word;

//Output
//my full name

#1


3  

A different approach to REGEX will be to iterate over the word and check if there is an uppercase letter, insert an space before and lowercase the letter

REGEX的另一种方法是迭代单词并检查是否有大写字母,在字母之前插入空格和小写字母

$label = 'myFullName';

$new_word='';

for($i=0, $n=strlen($label); $i<$n; $i++){

     $new_word .= strtoupper($label[$i]) == $label[$i] 
        ? ' '.strtolower($label[$i]) 
        : $label[$i];
}

echo $new_word;

//Output
//my full name