php用大写字母分隔字符串


php separate string by capitals

例如,我有这个字符串:"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);
?>

结果:

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

如果要将ATest分为ATest,请使用/[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);
?>

结果:

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