如何通过数组而不是字符串来爆炸字符串?

时间:2021-08-01 22:05:11

I have a string like 012A345B67Z89 that I need to explode by any letter (A-Z).

我有一个像012A345B67Z89这样的字符串,我需要用任何字母(A-Z)来爆炸。

The result I'm looking for is something like this:

我正在寻找的结果是这样的:

$str = '012A345B67Z89';
$result = explode(range('A','Z'),$str);
print_r($result);

and get:

array(
    [0] = 012
    [1] = 345
    [2] = 67
    [3] = 89
)

Ideally in php.

理想情况下在PHP中。

1 个解决方案

#1


4  

Try preg_split:

$str = '012A345B67Z89';
$result = preg_split("/[a-z]/i",$str);
print_r($result);

That should give you the exact output you want (sans the commas):

这应该给你你想要的确切输出(没有逗号):

Array
(
    [0] => 012
    [1] => 345
    [2] => 67
    [3] => 89
)

#1


4  

Try preg_split:

$str = '012A345B67Z89';
$result = preg_split("/[a-z]/i",$str);
print_r($result);

That should give you the exact output you want (sans the commas):

这应该给你你想要的确切输出(没有逗号):

Array
(
    [0] => 012
    [1] => 345
    [2] => 67
    [3] => 89
)