gpt4 book ai didi

php用大写分隔字符串

转载 作者:行者123 更新时间:2023-12-04 05:52:58 26 4
gpt4 key购买 nike

例如,我有这个字符串:"iCanSeeBluePeople"我需要它通过 将它分成数组大写字母第一个词 开头小写所以我会收到像 ["i","Can","See","Blue","People"] 这样的数组

字符串可以像 "grandPrix2009" => ["grand","Prix","2009"] , "dog" => ["dog"] , "aDog" => ["a","Dog"]等等

我发现这段代码工作正常,但我不适用于数字并忽略第一个小写字母:

<?
$str="MustangBlueHeadlining";

preg_match_all('/[A-Z][^A-Z]*/',$str,$results);
?>

感谢帮助

最佳答案

您可以使用正则表达式 /[a-z]+|[A-Z]+[a-z]*|[0-9]+/ .

<?
$str="thisIsATestVariableNumber000";
preg_match_all('/[a-z]+|[A-Z]+[a-z]*|[0-9]+/',$str,$results);
print_r($results);
?>

Result :
Array
(
[0] => Array
(
[0] => this
[1] => Is
[2] => ATest
[3] => Variable
[4] => Number
[5] => 000
)

)

使用 /[a-z]+|[A-Z][a-z]*|[0-9]+/如果你想要 ATest分为 ATest .
<?
$str="thisIsATestVariableNumber000";
preg_match_all('/[a-z]+|[A-Z][a-z]*|[0-9]+/',$str,$results);
print_r($results);
?>

Result :
Array
(
[0] => Array
(
[0] => this
[1] => Is
[2] => A
[3] => Test
[4] => Variable
[5] => Number
[6] => 000
)

)

关于php用大写分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com