Ryan
Ryan

Reputation: 15270

How can I explode a string by an array instead of a string?

I have a string like 012A345B67Z89 that I need to explode by any letter (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.

Upvotes: 1

Views: 99

Answers (1)

pp19dd
pp19dd

Reputation: 3633

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
)

Upvotes: 4

Related Questions