php由大写字母分隔字符串

时间:2012-03-25 00:47:42

标签: php regex string

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

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

您可以使用正则表达式/[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]+/分为ATestA,请使用Test

<?
    $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
    )

)